|
| 1 | +/*! |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * This module focuses on the validation of shared credentials properties |
| 6 | + */ |
| 7 | + |
| 8 | +import { localize } from 'vscode-nls' |
| 9 | +import { SectionName, SharedCredentialsKeys, StaticCredentialsProfileData } from './types' |
| 10 | +import { ToolkitError } from '../shared/errors' |
| 11 | +import { profileExists } from './sharedCredentials' |
| 12 | + |
| 13 | +/** |
| 14 | + * The format validators for shared credentials keys. |
| 15 | + * |
| 16 | + * A format validator validates the format of the data, |
| 17 | + * but not the validity of the content. |
| 18 | + */ |
| 19 | +export const CredentialsKeyFormatValidators = { |
| 20 | + [SharedCredentialsKeys.AWS_ACCESS_KEY_ID]: getAccessKeyIdFormatError, |
| 21 | + [SharedCredentialsKeys.AWS_SECRET_ACCESS_KEY]: getSecretAccessKeyFormatError, |
| 22 | +} as const |
| 23 | + |
| 24 | +/** |
| 25 | + * Holds the error for each key of static credentials data, |
| 26 | + * if it exists. This allows the user to get all the errors |
| 27 | + * at once. |
| 28 | + */ |
| 29 | +export type StaticCredentialsErrorResult = { |
| 30 | + [k in keyof StaticCredentialsProfileData]: string | undefined |
| 31 | +} |
| 32 | + |
| 33 | +export function getStaticCredentialsDataErrors( |
| 34 | + data: StaticCredentialsProfileData |
| 35 | +): StaticCredentialsErrorResult | undefined { |
| 36 | + const accessKeyIdError = CredentialsKeyFormatValidators[SharedCredentialsKeys.AWS_ACCESS_KEY_ID]( |
| 37 | + data[SharedCredentialsKeys.AWS_ACCESS_KEY_ID] |
| 38 | + ) |
| 39 | + const secretAccessKeyError = CredentialsKeyFormatValidators[SharedCredentialsKeys.AWS_SECRET_ACCESS_KEY]( |
| 40 | + data[SharedCredentialsKeys.AWS_SECRET_ACCESS_KEY] |
| 41 | + ) |
| 42 | + |
| 43 | + if (accessKeyIdError === undefined && secretAccessKeyError === undefined) { |
| 44 | + return undefined |
| 45 | + } |
| 46 | + |
| 47 | + return { |
| 48 | + [SharedCredentialsKeys.AWS_ACCESS_KEY_ID]: accessKeyIdError, |
| 49 | + [SharedCredentialsKeys.AWS_SECRET_ACCESS_KEY]: secretAccessKeyError, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +const accessKeyPattern = /[\w]{16,128}/ |
| 54 | + |
| 55 | +function getAccessKeyIdFormatError(awsAccessKeyId: string | undefined): string | undefined { |
| 56 | + if (awsAccessKeyId === undefined) { |
| 57 | + return undefined |
| 58 | + } |
| 59 | + |
| 60 | + if (awsAccessKeyId === '') { |
| 61 | + return localize('AWS.credentials.error.emptyAccessKey', 'Access key must not be empty') |
| 62 | + } |
| 63 | + if (!accessKeyPattern.test(awsAccessKeyId)) { |
| 64 | + return localize( |
| 65 | + 'AWS.credentials.error.emptyAccessKey', |
| 66 | + 'Access key must be alphanumeric and between 16 and 128 characters' |
| 67 | + ) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +function getSecretAccessKeyFormatError(awsSecretAccessKey: string | undefined): string | undefined { |
| 72 | + if (awsSecretAccessKey === undefined) { |
| 73 | + return undefined |
| 74 | + } |
| 75 | + |
| 76 | + if (awsSecretAccessKey === '') { |
| 77 | + return localize('AWS.credentials.error.emptySecretKey', 'Secret key must not be empty') |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** To be used as a sanity check to validate all core parts of a credentials profile */ |
| 82 | +export async function validateCredentialsProfile(profileName: SectionName, profileData: StaticCredentialsProfileData) { |
| 83 | + if (await profileExists(profileName)) { |
| 84 | + throw new ToolkitError(`Credentials profile "${profileName}" already exists`) |
| 85 | + } |
| 86 | + |
| 87 | + const credentialsDataErrors = getStaticCredentialsDataErrors(profileData) |
| 88 | + if (credentialsDataErrors !== undefined) { |
| 89 | + throw new ToolkitError(`Errors in credentials data: ${credentialsDataErrors}`, { |
| 90 | + code: 'InvalidCredentialsData', |
| 91 | + details: credentialsDataErrors, |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
0 commit comments