Skip to content

Commit

Permalink
add method to get channel from user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
maidul98 committed Jan 16, 2023
1 parent 3ba636f commit b8fa5e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 8 additions & 6 deletions backend/src/controllers/v2/secretsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { eventPushSecrets } from '../../events';
import { EESecretService, EELogService } from '../../ee/services';
import { postHogClient } from '../../services';
import { BadRequestError } from '../../utils/errors';
import { getChannelFromUserAgent } from '../../utils/posthog';

/**
* Create secret(s) for workspace with id [workspaceId] and environment [environment]
Expand Down Expand Up @@ -75,7 +76,7 @@ export const createSecrets = async (req: Request, res: Response) => {
}
}
*/
const channel = req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli';
const channel = getChannelFromUserAgent(req.headers['user-agent'])
const { workspaceId, environment } = req.body;

let toAdd;
Expand Down Expand Up @@ -194,7 +195,7 @@ export const createSecrets = async (req: Request, res: Response) => {
numberOfSecrets: toAdd.length,
environment,
workspaceId,
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
channel: channel,
userAgent: req.headers?.['user-agent']
}
});
Expand Down Expand Up @@ -280,7 +281,7 @@ export const getSecrets = async (req: Request, res: Response) => {

if (err) throw ValidationError({ message: 'Failed to get secrets', stack: err.stack });

const channel = req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli';
const channel = getChannelFromUserAgent(req.headers['user-agent'])

const readAction = await EELogService.createActionSecret({
name: ACTION_READ_SECRETS,
Expand Down Expand Up @@ -368,6 +369,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
*/
const channel = req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli';


// TODO: move type
interface PatchSecret {
id: string;
Expand Down Expand Up @@ -516,7 +518,7 @@ export const updateSecrets = async (req: Request, res: Response) => {
numberOfSecrets: workspaceSecretObj[key].length,
environment: workspaceSecretObj[key][0].environment,
workspaceId: key,
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
channel: channel,
userAgent: req.headers?.['user-agent']
}
});
Expand Down Expand Up @@ -582,7 +584,7 @@ export const deleteSecrets = async (req: Request, res: Response) => {
}
}
*/
const channel = req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli';
const channel = getChannelFromUserAgent(req.headers['user-agent'])
const toDelete = req.secrets.map((s: any) => s._id);

await Secret.deleteMany({
Expand Down Expand Up @@ -642,7 +644,7 @@ export const deleteSecrets = async (req: Request, res: Response) => {
numberOfSecrets: workspaceSecretObj[key].length,
environment: workspaceSecretObj[key][0].environment,
workspaceId: key,
channel: req.headers?.['user-agent']?.toLowerCase().includes('mozilla') ? 'web' : 'cli',
channel: channel,
userAgent: req.headers?.['user-agent']
}
});
Expand Down
15 changes: 15 additions & 0 deletions backend/src/utils/posthog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const CLI_USER_AGENT_NAME = "cli"
const K8_OPERATOR_AGENT_NAME = "k8-operator"
export const getChannelFromUserAgent = function (userAgent: string | undefined) {
if (userAgent == undefined) {
return "other"
} else if (userAgent == CLI_USER_AGENT_NAME) {
return "cli"
} else if (userAgent == K8_OPERATOR_AGENT_NAME) {
return "k8-operator"
} else if (userAgent.toLowerCase().includes('mozilla')) {
return "web"
} else {
return "other"
}
}

0 comments on commit b8fa5e8

Please sign in to comment.