Skip to content

Commit

Permalink
Merge pull request #2723 from rocketeerbkw/2722-fix-getSqlClient-not-…
Browse files Browse the repository at this point in the history
…defined

Fix mariasql related regression from #2690
  • Loading branch information
Schnitzel authored Jun 28, 2021
2 parents 7463b0d + e71c5e1 commit 6890db5
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions services/api/src/apolloServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,59 @@ const EnvironmentModel = require('./models/environment');

const schema = makeExecutableSchema({ typeDefs, resolvers });

const getGrantOrLegacyCredsFromToken = async (token) => {
const getGrantOrLegacyCredsFromToken = async token => {
let grant, legacyCredentials;

const sqlClientKeycloak = getSqlClient();
try {
grant = await getGrantForKeycloakToken(sqlClientKeycloak, token);
grant = await getGrantForKeycloakToken(token);

if (grant.access_token) {
const userActivityLogger = getUserActivityLogger(
grant ? grant.access_token.content : null
);

const { sub: currentUserId, azp: source, preferred_username, email, aud } = grant.access_token.content;
const {
sub: currentUserId,
azp: source,
preferred_username,
email,
aud
} = grant.access_token.content;
const username = preferred_username ? preferred_username : 'unknown';

userActivityLogger.user_auth(`Authentication granted for '${username} (${email ? email : 'unknown'})' from '${source}'`);
userActivityLogger.user_auth(
`Authentication granted for '${username} (${
email ? email : 'unknown'
})' from '${source}'`
);
}
sqlClientKeycloak.end();
} catch (e) {
// It might be a legacy token, so continue on.
sqlClientKeycloak.end();
logger.debug(`Keycloak token auth failed: ${e.message}`);
}

const sqlClientLegacy = getSqlClient();
try {
if (!grant) {
legacyCredentials = await getCredentialsForLegacyToken(
sqlClientLegacy,
token
);
legacyCredentials = await getCredentialsForLegacyToken(token);

const userActivityLogger = getUserActivityLogger(legacyCredentials);
const { sub, iss } = legacyCredentials;
const username = sub ? sub : 'unknown';
const source = iss ? iss : 'unknown';
userActivityLogger.user_auth(`Authentication granted for '${username}' from '${source}'`);

sqlClientLegacy.end();
userActivityLogger.user_auth(
`Authentication granted for '${username}' from '${source}'`
);
}
} catch (e) {
sqlClientLegacy.end();
logger.debug(`Keycloak legacy auth failed: ${e.message}`);
throw new AuthenticationError(e.message);
}

return {
grant: grant ? grant : null,
legacyCredentials: legacyCredentials ? legacyCredentials : null
}
}
};
};

const apolloServer = new ApolloServer({
schema,
Expand All @@ -95,7 +97,9 @@ const apolloServer = new ApolloServer({
throw new AuthenticationError('Auth token missing.');
}

const { grant, legacyCredentials } = await getGrantOrLegacyCredsFromToken(token);
const { grant, legacyCredentials } = await getGrantOrLegacyCredsFromToken(
token
);
const keycloakAdminClient = await getKeycloakAdminClient();
const requestCache = new NodeCache({
stdTTL: 0,
Expand Down Expand Up @@ -165,9 +169,11 @@ const apolloServer = new ApolloServer({
keycloakGrant: req.kauth ? req.kauth.grant : null,
requestCache,
userActivityLogger: getUserActivityLogger(
req.kauth ?
req.kauth.grant
: req.legacyCredentials ? req.legacyCredentials : null,
req.kauth
? req.kauth.grant
: req.legacyCredentials
? req.legacyCredentials
: null,
req.headers
),
models: {
Expand Down

0 comments on commit 6890db5

Please sign in to comment.