Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Import groups for a new user #24

Merged
merged 1 commit into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ The following parameters can be set in config files or in env variables:
- ELASTICCLOUD_ID: The elastic cloud id, if your elasticsearch instance is hosted on elastic cloud. DO NOT provide a value for ES_HOST if you are using this
- ELASTICCLOUD_USERNAME: The elastic cloud username for basic authentication. Provide this only if your elasticsearch instance is hosted on elastic cloud
- ELASTICCLOUD_PASSWORD: The elastic cloud password for basic authentication. Provide this only if your elasticsearch instance is hosted on elastic cloud
- AUTH0_URL: The auth0 url, Default is 'https://topcoder-dev.auth0.com/oauth/token'
- AUTH0_AUDIENCE: The auth0 audience for accessing ubahn api(s), Default is 'https://m2m.topcoder-dev.com/'
- AUTH0_CLIENT_ID: The auth0 client id
- AUTH0_CLIENT_SECRET: The auth0 client secret
- AUTH0_PROXY_SERVER_URL: The auth0 proxy server url
- TOKEN_CACHE_TIME: The token cache time
- TOPCODER_GROUP_API: The topcoder groups api, Default is 'https://api.topcoder-dev.com/v5/groups'

There is a `/health` endpoint that checks for the health of the app. This sets up an expressjs server and listens on the environment variable `PORT`. It's not part of the configuration file and needs to be passed as an environment variable

Expand Down
9 changes: 9 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ module.exports = {
// Kafka group id
KAFKA_GROUP_ID: process.env.KAFKA_GROUP_ID || 'ubahn-processor-es',

TOPCODER_GROUP_API: process.env.TOPCODER_GROUP_API || 'https://api.topcoder-dev.com/v5/groups',

AUTH0_URL: process.env.AUTH0_URL || 'https://topcoder-dev.auth0.com/oauth/token', // Auth0 credentials
AUTH0_AUDIENCE: process.env.AUTH0_AUDIENCE || 'https://m2m.topcoder-dev.com/',
TOKEN_CACHE_TIME: process.env.TOKEN_CACHE_TIME,
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
AUTH0_CLIENT_SECRET: process.env.AUTH0_CLIENT_SECRET,
AUTH0_PROXY_SERVER_URL: process.env.AUTH0_PROXY_SERVER_URL,

UBAHN_CREATE_TOPIC: process.env.UBAHN_CREATE_TOPIC || 'u-bahn.action.create',
UBAHN_UPDATE_TOPIC: process.env.UBAHN_UPDATE_TOPIC || 'u-bahn.action.update',
UBAHN_DELETE_TOPIC: process.env.UBAHN_DELETE_TOPIC || 'u-bahn.action.delete',
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"get-parameter-names": "^0.3.0",
"lodash": "^4.17.19",
"no-kafka": "^3.4.3",
"axios": "^0.19.2",
"tc-core-library-js": "appirio-tech/tc-core-library-js.git#v2.6",
"topcoder-healthcheck-dropin": "^1.0.3",
"winston": "^3.2.1"
},
Expand Down
33 changes: 33 additions & 0 deletions src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const elasticsearch = require('@elastic/elasticsearch')
const _ = require('lodash')
const Joi = require('@hapi/joi')
const { Mutex } = require('async-mutex')
const axios = require('axios')
const logger = require('./logger')
const m2mAuth = require('tc-core-library-js').auth.m2m
const topcoderM2M = m2mAuth(_.pick(config, ['AUTH0_URL', 'AUTH0_AUDIENCE', 'TOKEN_CACHE_TIME', 'AUTH0_PROXY_SERVER_URL']))

AWS.config.region = config.ES.AWS_REGION

Expand All @@ -18,6 +22,34 @@ let transactionId
const esClientMutex = new Mutex()
const mutexReleaseMap = {}

/* Function to get M2M token
* (Topcoder APIs only)
* @returns {Promise}
*/
async function getTopcoderM2Mtoken () {
return topcoderM2M.getMachineToken(config.AUTH0_CLIENT_ID, config.AUTH0_CLIENT_SECRET)
}

/**
* Returns the user in Topcoder identified by the email
* @param {String} email The user email
*/
async function getUserGroup (memberId) {
const url = config.TOPCODER_GROUP_API
const token = await getTopcoderM2Mtoken()
const params = { memberId, membershipType: 'user', page: 1 }

logger.debug(`request GET ${url} with params: ${JSON.stringify(params)}`)
let groups = []
let groupRes = await axios.get(url, { headers: { Authorization: `Bearer ${token}` }, params })
while (groupRes.data.length > 0) {
groups = _.concat(groups, _.map(groupRes.data, g => _.pick(g, 'id', 'name')))
params.page = params.page + 1
groupRes = await axios.get(url, { headers: { Authorization: `Bearer ${token}` }, params })
}
return groups
}

/**
* Get Kafka options
* @return {Object} the Kafka options
Expand Down Expand Up @@ -201,6 +233,7 @@ module.exports = {
getESClient,
validProperties,
getUser,
getUserGroup,
updateUser,
getOrg,
updateOrg,
Expand Down
6 changes: 6 additions & 0 deletions src/services/ProcessorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ async function processCreate (message, transactionId) {
user[userResource.propertyName] = []
}

// import groups for a new user
if (resource === 'externalprofile' && message.payload.externalId) {
const userGroups = await helper.getUserGroup(message.payload.externalId)
user[config.get('ES.USER_GROUP_PROPERTY_NAME')] = _.unionBy(user[config.get('ES.USER_GROUP_PROPERTY_NAME')], userGroups, 'id')
}

// check the resource does not exist
if (_.some(user[userResource.propertyName], [userResource.relateKey, relateId])) {
logger.error(`Can't create existed ${resource} with the ${userResource.relateKey}: ${relateId}, userId: ${message.payload.userId}`)
Expand Down