Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: add source user and type to tasks and deployments #3631

Merged
merged 1 commit into from
Feb 14, 2024
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
24 changes: 20 additions & 4 deletions node-packages/commons/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1367,11 +1367,15 @@ export const addDeployment = (
completed: string = null,
priority: number = null,
bulkId: string = null,
bulkName: string = null
bulkName: string = null,
sourceUser = null,
sourceType = null,
): Promise<any> =>
graphqlapi.mutate(
`
($name: String!, $status: DeploymentStatusType!, $created: String!, $environment: Int!, $id: Int, $remoteId: String, $started: String, $completed: String, $priority: Int, $bulkId: String, $bulkName: String) {
($name: String!, $status: DeploymentStatusType!, $created: String!, $environment: Int!, $id: Int, $remoteId: String,
$started: String, $completed: String, $priority: Int, $bulkId: String, $bulkName: String,
$sourceUser: String, $sourceType: DeploymentSourceType) {
addDeployment(input: {
name: $name
status: $status
Expand All @@ -1384,6 +1388,8 @@ export const addDeployment = (
priority: $priority
bulkId: $bulkId
bulkName: $bulkName
sourceUser: $sourceUser
sourceType: $sourceType
}) {
...${deploymentFragment}
}
Expand All @@ -1400,7 +1406,9 @@ export const addDeployment = (
completed,
priority,
bulkId,
bulkName
bulkName,
sourceUser,
sourceType,
}
);

Expand All @@ -1416,10 +1424,14 @@ export const addDeployment = (
service = null,
command = null,
execute = false,
sourceUser = null,
sourceType = null,
) =>
graphqlapi.mutate(
`
($name: String!, $status: TaskStatusType!, $created: String!, $environment: Int!, $id: Int, $remoteId: String, $started: String, $completed: String, $service: String, $command: String, $execute: Boolean) {
($name: String!, $status: TaskStatusType!, $created: String!, $environment: Int!, $id: Int, $remoteId: String,
$started: String, $completed: String, $service: String, $command: String, $execute: Boolean,
$sourceUser: String, $sourceType: TaskSourceType) {
addTask(input: {
name: $name
status: $status
Expand All @@ -1432,6 +1444,8 @@ export const addDeployment = (
service: $service
command: $command
execute: $execute
sourceUser: $sourceUser
sourceType: $sourceType
}) {
...${taskFragment}
}
Expand All @@ -1449,6 +1463,8 @@ export const addDeployment = (
service,
command,
execute,
sourceUser,
sourceType,
},
);

Expand Down
8 changes: 6 additions & 2 deletions node-packages/commons/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ export const getControllerBuildData = async function(deployData: any) {
buildPriority,
bulkId,
bulkName,
buildVariables
buildVariables,
sourceUser,
sourceType,
} = deployData;

var environmentName = makeSafe(branchName)
Expand Down Expand Up @@ -593,7 +595,9 @@ export const getControllerBuildData = async function(deployData: any) {
null, null, null, null,
buildPriority,
bulkId,
bulkName
bulkName,
sourceUser,
sourceType,
);
} catch (error) {
logger.error(`Could not save deployment for project ${lagoonProjectData.id}. Message: ${error}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function(knex) {
return knex.schema
.alterTable('task', function (table) {
table.enu('source_type', ['api']);
shreddedbacon marked this conversation as resolved.
Show resolved Hide resolved
table.string('source_user', 300);
})
.alterTable('deployment', function (table) {
table.enu('source_type', ['api', 'webhook']);
table.string('source_user', 300);
})
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function(knex) {
return knex.schema
.alterTable('task', (table) => {
table.dropColumn('source_type');
table.dropColumn('source_user');
})
.alterTable('deployment', (table) => {
table.dropColumn('source_type');
table.dropColumn('source_user');
})
};
4 changes: 4 additions & 0 deletions services/api/src/apolloServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const apolloServer = new ApolloServer({
let keycloakUsersGroups = []
let groupRoleProjectIds = []
const keycloakGrant = grant
let legacyGrant = legacyCredentials ? legacyCredentials : null
if (keycloakGrant) {
keycloakUsersGroups = await User.User(modelClients).getAllGroupsForUser(keycloakGrant.access_token.content.sub);
serviceAccount = await keycloakGrantManager.obtainFromClientCredentials();
Expand All @@ -164,6 +165,7 @@ const apolloServer = new ApolloServer({
? keycloakHasPermission(grant, requestCache, modelClients, serviceAccount, currentUser, groupRoleProjectIds)
: legacyHasPermission(legacyCredentials),
keycloakGrant,
legacyGrant,
requestCache,
models: {
UserModel: User.User(modelClients),
Expand Down Expand Up @@ -233,6 +235,7 @@ const apolloServer = new ApolloServer({
let keycloakUsersGroups = []
let groupRoleProjectIds = []
const keycloakGrant = req.kauth ? req.kauth.grant : null
let legacyGrant = req.legacyCredentials ? req.legacyCredentials : null
if (keycloakGrant) {
keycloakUsersGroups = await User.User(modelClients).getAllGroupsForUser(keycloakGrant.access_token.content.sub);
serviceAccount = await keycloakGrantManager.obtainFromClientCredentials();
Expand Down Expand Up @@ -284,6 +287,7 @@ const apolloServer = new ApolloServer({
hasPermission,
keycloakGrant,
requestCache,
legacyGrant,
userActivityLogger: (message, meta) => {
let defaultMeta = {
user: req.kauth
Expand Down
7 changes: 7 additions & 0 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ const resolvers = {
MAINTAINER: 'maintainer',
OWNER: 'owner'
},
DeploymentSourceType: {
API: 'api',
WEBHOOK: 'webhook'
},
TaskSourceType: {
API: 'api',
},
ProjectOrderType: {
NAME: 'name',
CREATED: 'created'
Expand Down
14 changes: 14 additions & 0 deletions services/api/src/resources/deployment/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@ export const Helpers = (sqlClientPool: Pool) => {
return R.prop(0, rows);
};

// getSourceUser can decode the keycloak or legacy grant into a username or issuer name
// this can then be stored against the deployment (or task) resource in the API when it is created
const getSourceUser =async (keycloakGrant, legacyGrant) => {
let sourceUser = "administrator"
if (keycloakGrant) {
sourceUser = keycloakGrant.access_token.content.email
}
if (legacyGrant) {
sourceUser = legacyGrant.iss
}
return sourceUser
}

return {
getSourceUser,
getDeploymentById,
getDeploymentByDeploymentInput: async deploymentInput => {
const notEmpty = R.complement(R.anyPass([R.isNil, R.isEmpty]));
Expand Down
45 changes: 35 additions & 10 deletions services/api/src/resources/deployment/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,12 @@ export const addDeployment: ResolverFn = async (
priority,
bulkId,
bulkName,
buildStep
buildStep,
sourceUser,
sourceType,
}
},
{ sqlClientPool, hasPermission, userActivityLogger }
{ sqlClientPool, hasPermission, userActivityLogger, keycloakGrant, legacyGrant }
) => {
const environment = await environmentHelpers(
sqlClientPool
Expand All @@ -352,6 +354,12 @@ export const addDeployment: ResolverFn = async (
project: environment.project
});

if (!sourceUser) {
sourceUser = await Helpers(sqlClientPool).getSourceUser(keycloakGrant, legacyGrant)
}
if (!sourceType) {
sourceType = "API"
}
const { insertId } = await query(
sqlClientPool,
Sql.insertDeployment({
Expand All @@ -366,7 +374,9 @@ export const addDeployment: ResolverFn = async (
priority,
bulkId,
bulkName,
buildStep
buildStep,
sourceType,
sourceUser,
})
);

Expand Down Expand Up @@ -606,7 +616,7 @@ export const deployEnvironmentLatest: ResolverFn = async (
returnData
}
},
{ sqlClientPool, hasPermission, userActivityLogger }
{ sqlClientPool, hasPermission, userActivityLogger, keycloakGrant, legacyGrant }
) => {

try {
Expand Down Expand Up @@ -671,7 +681,7 @@ export const deployEnvironmentLatest: ResolverFn = async (
}

let buildName = generateBuildId();

const sourceUser = await Helpers(sqlClientPool).getSourceUser(keycloakGrant, legacyGrant)
let deployData: {
[key: string]: any;
} = {
Expand All @@ -682,6 +692,8 @@ export const deployEnvironmentLatest: ResolverFn = async (
bulkId: bulkId,
bulkName: bulkName,
buildVariables: buildVariables,
sourceType: "API",
sourceUser: sourceUser
};
let meta: {
[key: string]: any;
Expand Down Expand Up @@ -808,7 +820,7 @@ export const deployEnvironmentBranch: ResolverFn = async (
returnData
}
},
{ sqlClientPool, hasPermission, userActivityLogger }
{ sqlClientPool, hasPermission, userActivityLogger, keycloakGrant, legacyGrant }
) => {
const project = await projectHelpers(sqlClientPool).getProjectByProjectInput(
projectInput
Expand All @@ -825,6 +837,7 @@ export const deployEnvironmentBranch: ResolverFn = async (
}

let buildName = generateBuildId();
const sourceUser = await Helpers(sqlClientPool).getSourceUser(keycloakGrant, legacyGrant)

const deployData = {
type: 'branch',
Expand All @@ -836,6 +849,8 @@ export const deployEnvironmentBranch: ResolverFn = async (
bulkId: bulkId,
bulkName: bulkName,
buildVariables: buildVariables,
sourceType: "API",
sourceUser: sourceUser
};

const meta = {
Expand Down Expand Up @@ -912,7 +927,7 @@ export const deployEnvironmentPullrequest: ResolverFn = async (
returnData
}
},
{ sqlClientPool, hasPermission, userActivityLogger }
{ sqlClientPool, hasPermission, userActivityLogger, keycloakGrant, legacyGrant }
) => {
const branchName = `pr-${number}`;
const project = await projectHelpers(sqlClientPool).getProjectByProjectInput(
Expand All @@ -931,6 +946,7 @@ export const deployEnvironmentPullrequest: ResolverFn = async (

let buildName = generateBuildId();

const sourceUser = await Helpers(sqlClientPool).getSourceUser(keycloakGrant, legacyGrant)
const deployData = {
type: 'pullrequest',
projectName: project.name,
Expand All @@ -946,6 +962,8 @@ export const deployEnvironmentPullrequest: ResolverFn = async (
bulkId: bulkId,
bulkName: bulkName,
buildVariables: buildVariables,
sourceType: "API",
sourceUser: sourceUser
};

const meta = {
Expand Down Expand Up @@ -1018,7 +1036,7 @@ export const deployEnvironmentPromote: ResolverFn = async (
returnData
}
},
{ sqlClientPool, hasPermission, userActivityLogger }
{ sqlClientPool, hasPermission, userActivityLogger, keycloakGrant, legacyGrant }
) => {
const destProject = await projectHelpers(
sqlClientPool
Expand Down Expand Up @@ -1056,6 +1074,7 @@ export const deployEnvironmentPromote: ResolverFn = async (

let buildName = generateBuildId();

const sourceUser = await Helpers(sqlClientPool).getSourceUser(keycloakGrant, legacyGrant)
const deployData = {
type: 'promote',
projectName: destProject.name,
Expand All @@ -1066,6 +1085,8 @@ export const deployEnvironmentPromote: ResolverFn = async (
bulkId: bulkId,
bulkName: bulkName,
buildVariables: buildVariables,
sourceType: "API",
sourceUser: sourceUser
};

const meta = {
Expand Down Expand Up @@ -1129,7 +1150,7 @@ export const deployEnvironmentPromote: ResolverFn = async (
export const switchActiveStandby: ResolverFn = async (
root,
{ input: { project: projectInput } },
{ sqlClientPool, hasPermission }
{ sqlClientPool, hasPermission, keycloakGrant, legacyGrant }
) => {
const project = await projectHelpers(sqlClientPool).getProjectByProjectInput(
projectInput
Expand Down Expand Up @@ -1227,6 +1248,8 @@ export const switchActiveStandby: ResolverFn = async (
};

// try it now
const sourceUser = await Helpers(sqlClientPool).getSourceUser(keycloakGrant, legacyGrant)
const sourceType = "API"
try {
// add a task into the environment
var date = new Date();
Expand All @@ -1242,7 +1265,9 @@ export const switchActiveStandby: ResolverFn = async (
null,
'',
'',
false
false,
sourceUser,
sourceType,
);
data.task.id = sourceTaskData.addTask.id.toString();

Expand Down
6 changes: 6 additions & 0 deletions services/api/src/resources/deployment/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export const Sql = {
bulkId,
bulkName,
buildStep,
sourceType,
sourceUser,
}: {
id: number,
name: string,
Expand All @@ -36,6 +38,8 @@ export const Sql = {
bulkId: string,
bulkName: string,
buildStep: string,
sourceType?: string,
sourceUser?: string,
}) =>
knex('deployment')
.insert({
Expand All @@ -51,6 +55,8 @@ export const Sql = {
bulkId,
bulkName,
buildStep,
sourceType,
sourceUser,
})
.toString(),
deleteDeployment: (id: number) =>
Expand Down
Loading