@goldstack/infra-aws 0.3.49 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,10 @@
1
- import { AWSConfiguration } from './types/awsAccount';
2
- import AWS from 'aws-sdk';
3
- export declare function getAWSUserFromEnvironmentVariables(): Promise<AWS.Credentials>;
4
- /**
5
- * Obtains AWS user credentials from container environment variables for ECS containers.
6
- */
7
- export declare function getAWSUserFromContainerEnvironment(): Promise<AWS.ECSCredentials>;
8
- export declare function getAWSUserFromDefaultLocalProfile(): Promise<AWS.Credentials>;
9
- export declare function getAWSUserFromGoldstackConfig(config: AWSConfiguration, userName: string): Promise<AWS.Credentials>;
1
+ import { AWSConfiguration } from './types/awsAccount';
2
+ import AWS from 'aws-sdk';
3
+ export declare function getAWSUserFromEnvironmentVariables(): Promise<AWS.Credentials>;
4
+ /**
5
+ * Obtains AWS user credentials from container environment variables for ECS containers.
6
+ */
7
+ export declare function getAWSUserFromContainerEnvironment(): Promise<AWS.ECSCredentials>;
8
+ export declare function getAWSUserFromDefaultLocalProfile(): Promise<AWS.Credentials>;
9
+ export declare function getAWSUserFromGoldstackConfig(config: AWSConfiguration, userName: string): Promise<AWS.Credentials>;
10
10
  //# sourceMappingURL=awsUserUtils.d.ts.map
@@ -1,162 +1,162 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getAWSUserFromGoldstackConfig = exports.getAWSUserFromDefaultLocalProfile = exports.getAWSUserFromContainerEnvironment = exports.getAWSUserFromEnvironmentVariables = void 0;
7
- const assert_1 = __importDefault(require("assert"));
8
- const aws_sdk_1 = __importDefault(require("aws-sdk"));
9
- async function getAWSUserFromEnvironmentVariables() {
10
- (0, assert_1.default)(process.env.AWS_ACCESS_KEY_ID, 'AWS_ACCESS_KEY_ID not defined.');
11
- (0, assert_1.default)(process.env.AWS_SECRET_ACCESS_KEY, 'AWS_SECRET_ACCESS_KEY not defined');
12
- const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION;
13
- (0, assert_1.default)(region, 'Neither AWS_REGION nor AWS_DEFAULT_REGION are defined.');
14
- const credentials = new aws_sdk_1.default.EnvironmentCredentials('AWS');
15
- await credentials.getPromise();
16
- aws_sdk_1.default.config.credentials = credentials;
17
- aws_sdk_1.default.config.update({ region });
18
- return credentials;
19
- }
20
- exports.getAWSUserFromEnvironmentVariables = getAWSUserFromEnvironmentVariables;
21
- /**
22
- * Obtains AWS user credentials from container environment variables for ECS containers.
23
- */
24
- async function getAWSUserFromContainerEnvironment() {
25
- const ecsCredentials = new aws_sdk_1.default.ECSCredentials({
26
- httpOptions: { timeout: 5000 },
27
- maxRetries: 10, // retry 10 times
28
- });
29
- await ecsCredentials.getPromise();
30
- aws_sdk_1.default.config.credentials = ecsCredentials;
31
- if (!process.env.AWS_REGION) {
32
- throw new Error('AWS region environment variable ("AWS_REGION") not defined for ECS task.');
33
- }
34
- aws_sdk_1.default.config.update({ region: process.env.AWS_REGION });
35
- return ecsCredentials;
36
- }
37
- exports.getAWSUserFromContainerEnvironment = getAWSUserFromContainerEnvironment;
38
- async function getAWSUserFromDefaultLocalProfile() {
39
- let credentials = new aws_sdk_1.default.SharedIniFileCredentials();
40
- const envVarValues = {
41
- AWS_SDK_LOAD_CONFIG: process.env.AWS_SDK_LOAD_CONFIG,
42
- };
43
- // if no access key is found, try loading process_credentials
44
- if (!credentials.accessKeyId) {
45
- // see https://github.com/aws/aws-sdk-js/pull/1391
46
- process.env.AWS_SDK_LOAD_CONFIG = '1';
47
- credentials = new aws_sdk_1.default.ProcessCredentials();
48
- await credentials.refreshPromise();
49
- }
50
- resetEnvironmentVariables(envVarValues);
51
- aws_sdk_1.default.config.credentials = credentials;
52
- return credentials;
53
- }
54
- exports.getAWSUserFromDefaultLocalProfile = getAWSUserFromDefaultLocalProfile;
55
- async function getAWSUserFromGoldstackConfig(config, userName) {
56
- const user = config.users.find((user) => user.name === userName);
57
- if (!user) {
58
- throw new Error(`User '${userName}' does not exist in AWS configuration.`);
59
- }
60
- if (user.type === 'profile') {
61
- const userConfig = user.config;
62
- if (process.env.AWS_SHARED_CREDENTIALS_FILE) {
63
- console.warn(`Using AWS_SHARED_CREDENTIALS_FILE environment variable: '${process.env.AWS_SHARED_CREDENTIALS_FILE}'. awsCredentialsFileName in configuration will be ignored.`);
64
- }
65
- const envVarValues = {
66
- AWS_SDK_LOAD_CONFIG: process.env.AWS_SDK_LOAD_CONFIG,
67
- AWS_SHARED_CREDENTIALS_FILE: process.env.AWS_SHARED_CREDENTIALS_FILE,
68
- AWS_CONFIG_FILE: process.env.AWS_CONFIG_FILE,
69
- };
70
- if (userConfig.awsConfigFileName) {
71
- // support loading from both `config` and `credentials` files, see https://github.com/goldstack/goldstack/issues/17#issuecomment-1044811805 https://github.com/aws/aws-sdk-js/pull/1391
72
- process.env.AWS_SDK_LOAD_CONFIG = '1';
73
- // filename property is ignored if AWS_SDK_LOAD_CONFIG is set; thus need to set AWS_SHARED_CREDENTIALS_FILE.
74
- process.env.AWS_SHARED_CREDENTIALS_FILE =
75
- userConfig.awsCredentialsFileName;
76
- process.env.AWS_CONFIG_FILE = userConfig.awsConfigFileName;
77
- }
78
- let credentials;
79
- let filename = undefined;
80
- if (!process.env.SHARE_CREDENTIALS_FILE) {
81
- filename = userConfig.awsCredentialsFileName;
82
- }
83
- if (userConfig.credentialsSource !== 'process') {
84
- credentials = new aws_sdk_1.default.SharedIniFileCredentials({
85
- profile: userConfig.profile,
86
- filename: filename,
87
- });
88
- }
89
- else {
90
- // Allow `AWS.ProcessCredentials` to search the default config location `~/.aws/config` in addition to `credentials`
91
- // This matches most other CLI / SDK implementations (including AWS JS SDK v3) and the behaviour of most `credential_process` helper tools
92
- // With this enabled, `AWS_CONFIG_FILE` must not contains an invalid path, but `AWS_SHARED_CREDENTIALS_FILE` can be missing.
93
- if (!userConfig.awsCredentialsFileName) {
94
- process.env.AWS_SDK_LOAD_CONFIG = '1';
95
- }
96
- credentials = new aws_sdk_1.default.ProcessCredentials({
97
- profile: userConfig.profile,
98
- filename: filename,
99
- });
100
- await credentials.refreshPromise();
101
- }
102
- resetEnvironmentVariables(envVarValues);
103
- if (!credentials.accessKeyId) {
104
- throw new Error('Cannot load profile ' +
105
- userConfig.profile +
106
- ' from AWS configuration for user ' +
107
- user.name +
108
- '. Please perform `aws login` for the profile using the AWS CLI.');
109
- }
110
- aws_sdk_1.default.config.credentials = credentials;
111
- aws_sdk_1.default.config.update({ region: userConfig.awsDefaultRegion });
112
- return credentials;
113
- }
114
- if (user.type === 'apiKey') {
115
- const config = user.config;
116
- if (!config.awsAccessKeyId || !config.awsSecretAccessKey) {
117
- throw new Error(`AWS Access credentials not defined for user ${userName}. Define them in infra/aws/config.json.`);
118
- }
119
- const credentials = new aws_sdk_1.default.Credentials({
120
- accessKeyId: config.awsAccessKeyId || '',
121
- secretAccessKey: config.awsSecretAccessKey || '',
122
- });
123
- aws_sdk_1.default.config.credentials = credentials;
124
- aws_sdk_1.default.config.update({ region: config.awsDefaultRegion });
125
- return credentials;
126
- }
127
- if (user.type === 'environmentVariables') {
128
- const userConfig = user.config;
129
- const awsAccessKeyId = process.env[userConfig.awsAccessKeyIdVariableName];
130
- if (!awsAccessKeyId) {
131
- throw new Error(`Environment variable expected but not found: ${userConfig.awsAccessKeyIdVariableName}`);
132
- }
133
- const awsSecretAccessKey = process.env[userConfig.awsSecretAccessKeyVariableName];
134
- if (!awsSecretAccessKey) {
135
- throw new Error(`Environment variable expected but not found: ${userConfig.awsSecretAccessKeyVariableName}`);
136
- }
137
- const awsDefaultRegion = process.env[userConfig.awsDefaultRegionVariableName];
138
- if (!awsDefaultRegion) {
139
- throw new Error(`Environment variable expected but not found: ${userConfig.awsDefaultRegionVariableName}`);
140
- }
141
- const credentials = new aws_sdk_1.default.Credentials({
142
- accessKeyId: awsAccessKeyId,
143
- secretAccessKey: awsSecretAccessKey,
144
- });
145
- aws_sdk_1.default.config.credentials = credentials;
146
- aws_sdk_1.default.config.update({ region: awsDefaultRegion });
147
- return credentials;
148
- }
149
- throw new Error(`Unknown user config type ${user.type}`);
150
- }
151
- exports.getAWSUserFromGoldstackConfig = getAWSUserFromGoldstackConfig;
152
- function resetEnvironmentVariables(envVarValues) {
153
- Object.entries(envVarValues).forEach(([key, value]) => {
154
- if (process.env[key] === undefined) {
155
- delete process.env[key];
156
- }
157
- else {
158
- process.env[key] = value;
159
- }
160
- });
161
- }
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getAWSUserFromGoldstackConfig = exports.getAWSUserFromDefaultLocalProfile = exports.getAWSUserFromContainerEnvironment = exports.getAWSUserFromEnvironmentVariables = void 0;
7
+ const assert_1 = __importDefault(require("assert"));
8
+ const aws_sdk_1 = __importDefault(require("aws-sdk"));
9
+ async function getAWSUserFromEnvironmentVariables() {
10
+ (0, assert_1.default)(process.env.AWS_ACCESS_KEY_ID, 'AWS_ACCESS_KEY_ID not defined.');
11
+ (0, assert_1.default)(process.env.AWS_SECRET_ACCESS_KEY, 'AWS_SECRET_ACCESS_KEY not defined');
12
+ const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION;
13
+ (0, assert_1.default)(region, 'Neither AWS_REGION nor AWS_DEFAULT_REGION are defined.');
14
+ const credentials = new aws_sdk_1.default.EnvironmentCredentials('AWS');
15
+ await credentials.getPromise();
16
+ aws_sdk_1.default.config.credentials = credentials;
17
+ aws_sdk_1.default.config.update({ region });
18
+ return credentials;
19
+ }
20
+ exports.getAWSUserFromEnvironmentVariables = getAWSUserFromEnvironmentVariables;
21
+ /**
22
+ * Obtains AWS user credentials from container environment variables for ECS containers.
23
+ */
24
+ async function getAWSUserFromContainerEnvironment() {
25
+ const ecsCredentials = new aws_sdk_1.default.ECSCredentials({
26
+ httpOptions: { timeout: 5000 },
27
+ maxRetries: 10, // retry 10 times
28
+ });
29
+ await ecsCredentials.getPromise();
30
+ aws_sdk_1.default.config.credentials = ecsCredentials;
31
+ if (!process.env.AWS_REGION) {
32
+ throw new Error('AWS region environment variable ("AWS_REGION") not defined for ECS task.');
33
+ }
34
+ aws_sdk_1.default.config.update({ region: process.env.AWS_REGION });
35
+ return ecsCredentials;
36
+ }
37
+ exports.getAWSUserFromContainerEnvironment = getAWSUserFromContainerEnvironment;
38
+ async function getAWSUserFromDefaultLocalProfile() {
39
+ let credentials = new aws_sdk_1.default.SharedIniFileCredentials();
40
+ const envVarValues = {
41
+ AWS_SDK_LOAD_CONFIG: process.env.AWS_SDK_LOAD_CONFIG,
42
+ };
43
+ // if no access key is found, try loading process_credentials
44
+ if (!credentials.accessKeyId) {
45
+ // see https://github.com/aws/aws-sdk-js/pull/1391
46
+ process.env.AWS_SDK_LOAD_CONFIG = '1';
47
+ credentials = new aws_sdk_1.default.ProcessCredentials();
48
+ await credentials.refreshPromise();
49
+ }
50
+ resetEnvironmentVariables(envVarValues);
51
+ aws_sdk_1.default.config.credentials = credentials;
52
+ return credentials;
53
+ }
54
+ exports.getAWSUserFromDefaultLocalProfile = getAWSUserFromDefaultLocalProfile;
55
+ async function getAWSUserFromGoldstackConfig(config, userName) {
56
+ const user = config.users.find((user) => user.name === userName);
57
+ if (!user) {
58
+ throw new Error(`User '${userName}' does not exist in AWS configuration.`);
59
+ }
60
+ if (user.type === 'profile') {
61
+ const userConfig = user.config;
62
+ if (process.env.AWS_SHARED_CREDENTIALS_FILE) {
63
+ console.warn(`Using AWS_SHARED_CREDENTIALS_FILE environment variable: '${process.env.AWS_SHARED_CREDENTIALS_FILE}'. awsCredentialsFileName in configuration will be ignored.`);
64
+ }
65
+ const envVarValues = {
66
+ AWS_SDK_LOAD_CONFIG: process.env.AWS_SDK_LOAD_CONFIG,
67
+ AWS_SHARED_CREDENTIALS_FILE: process.env.AWS_SHARED_CREDENTIALS_FILE,
68
+ AWS_CONFIG_FILE: process.env.AWS_CONFIG_FILE,
69
+ };
70
+ if (userConfig.awsConfigFileName) {
71
+ // support loading from both `config` and `credentials` files, see https://github.com/goldstack/goldstack/issues/17#issuecomment-1044811805 https://github.com/aws/aws-sdk-js/pull/1391
72
+ process.env.AWS_SDK_LOAD_CONFIG = '1';
73
+ // filename property is ignored if AWS_SDK_LOAD_CONFIG is set; thus need to set AWS_SHARED_CREDENTIALS_FILE.
74
+ process.env.AWS_SHARED_CREDENTIALS_FILE =
75
+ userConfig.awsCredentialsFileName;
76
+ process.env.AWS_CONFIG_FILE = userConfig.awsConfigFileName;
77
+ }
78
+ let credentials;
79
+ let filename = undefined;
80
+ if (!process.env.SHARE_CREDENTIALS_FILE) {
81
+ filename = userConfig.awsCredentialsFileName;
82
+ }
83
+ if (userConfig.credentialsSource !== 'process') {
84
+ credentials = new aws_sdk_1.default.SharedIniFileCredentials({
85
+ profile: userConfig.profile,
86
+ filename: filename,
87
+ });
88
+ }
89
+ else {
90
+ // Allow `AWS.ProcessCredentials` to search the default config location `~/.aws/config` in addition to `credentials`
91
+ // This matches most other CLI / SDK implementations (including AWS JS SDK v3) and the behaviour of most `credential_process` helper tools
92
+ // With this enabled, `AWS_CONFIG_FILE` must not contains an invalid path, but `AWS_SHARED_CREDENTIALS_FILE` can be missing.
93
+ if (!userConfig.awsCredentialsFileName) {
94
+ process.env.AWS_SDK_LOAD_CONFIG = '1';
95
+ }
96
+ credentials = new aws_sdk_1.default.ProcessCredentials({
97
+ profile: userConfig.profile,
98
+ filename: filename,
99
+ });
100
+ await credentials.refreshPromise();
101
+ }
102
+ resetEnvironmentVariables(envVarValues);
103
+ if (!credentials.accessKeyId) {
104
+ throw new Error('Cannot load profile ' +
105
+ userConfig.profile +
106
+ ' from AWS configuration for user ' +
107
+ user.name +
108
+ '. Please perform `aws login` for the profile using the AWS CLI.');
109
+ }
110
+ aws_sdk_1.default.config.credentials = credentials;
111
+ aws_sdk_1.default.config.update({ region: userConfig.awsDefaultRegion });
112
+ return credentials;
113
+ }
114
+ if (user.type === 'apiKey') {
115
+ const config = user.config;
116
+ if (!config.awsAccessKeyId || !config.awsSecretAccessKey) {
117
+ throw new Error(`AWS Access credentials not defined for user ${userName}. Define them in infra/aws/config.json.`);
118
+ }
119
+ const credentials = new aws_sdk_1.default.Credentials({
120
+ accessKeyId: config.awsAccessKeyId || '',
121
+ secretAccessKey: config.awsSecretAccessKey || '',
122
+ });
123
+ aws_sdk_1.default.config.credentials = credentials;
124
+ aws_sdk_1.default.config.update({ region: config.awsDefaultRegion });
125
+ return credentials;
126
+ }
127
+ if (user.type === 'environmentVariables') {
128
+ const userConfig = user.config;
129
+ const awsAccessKeyId = process.env[userConfig.awsAccessKeyIdVariableName];
130
+ if (!awsAccessKeyId) {
131
+ throw new Error(`Environment variable expected but not found: ${userConfig.awsAccessKeyIdVariableName}`);
132
+ }
133
+ const awsSecretAccessKey = process.env[userConfig.awsSecretAccessKeyVariableName];
134
+ if (!awsSecretAccessKey) {
135
+ throw new Error(`Environment variable expected but not found: ${userConfig.awsSecretAccessKeyVariableName}`);
136
+ }
137
+ const awsDefaultRegion = process.env[userConfig.awsDefaultRegionVariableName];
138
+ if (!awsDefaultRegion) {
139
+ throw new Error(`Environment variable expected but not found: ${userConfig.awsDefaultRegionVariableName}`);
140
+ }
141
+ const credentials = new aws_sdk_1.default.Credentials({
142
+ accessKeyId: awsAccessKeyId,
143
+ secretAccessKey: awsSecretAccessKey,
144
+ });
145
+ aws_sdk_1.default.config.credentials = credentials;
146
+ aws_sdk_1.default.config.update({ region: awsDefaultRegion });
147
+ return credentials;
148
+ }
149
+ throw new Error(`Unknown user config type ${user.type}`);
150
+ }
151
+ exports.getAWSUserFromGoldstackConfig = getAWSUserFromGoldstackConfig;
152
+ function resetEnvironmentVariables(envVarValues) {
153
+ Object.entries(envVarValues).forEach(([key, value]) => {
154
+ if (process.env[key] === undefined) {
155
+ delete process.env[key];
156
+ }
157
+ else {
158
+ process.env[key] = value;
159
+ }
160
+ });
161
+ }
162
162
  //# sourceMappingURL=awsUserUtils.js.map
@@ -1,19 +1,19 @@
1
- import { AWSConfiguration, AWSUser, AWSProfileConfig, AWSAPIKeyUserConfig, AWSAccessKeyId, AWSSecretAccessKey, Name } from './types/awsAccount';
2
- import AWS from 'aws-sdk';
3
- import { AWSTerraformState, RemoteState } from './types/awsTerraformState';
4
- import { AWSRegion, AWSEnvironmentVariableUserConfig } from './types/awsAccount';
5
- export type { AWSConfiguration, AWSUser, AWSRegion, AWSAccessKeyId, AWSSecretAccessKey, AWSProfileConfig as AWSLocalUserConfig, AWSAPIKeyUserConfig as AWSAPIKeyUser, AWSEnvironmentVariableUserConfig, AWSTerraformState, RemoteState, };
6
- import { AWSDeployment } from './types/awsDeployment';
7
- export type { AWSDeployment, AWSDeploymentRegion, AWSUserName, } from './types/awsDeployment';
8
- export declare const readDeploymentFromPackageConfig: (deploymentName: string, path?: string) => AWSDeployment;
9
- export declare const assertTerraformConfig: (user: Name, path?: string) => AWSTerraformState;
10
- export declare const writeTerraformConfig: (config: AWSTerraformState, path?: string) => void;
11
- export declare const hasConfig: (path?: string) => boolean;
12
- export declare const readConfig: (path?: string) => AWSConfiguration;
13
- export declare const writeConfig: (config: AWSConfiguration, path?: string) => void;
14
- export declare const createDefaultConfig: () => AWSConfiguration;
15
- /**
16
- * Obtains AWS user credentials from config file or environment variables.
17
- */
18
- export declare const getAWSUser: (userName: string, configPath?: string) => Promise<AWS.Credentials>;
1
+ import { AWSConfiguration, AWSUser, AWSProfileConfig, AWSAPIKeyUserConfig, AWSAccessKeyId, AWSSecretAccessKey, Name } from './types/awsAccount';
2
+ import AWS from 'aws-sdk';
3
+ import { AWSTerraformState, RemoteState } from './types/awsTerraformState';
4
+ import { AWSRegion, AWSEnvironmentVariableUserConfig } from './types/awsAccount';
5
+ export type { AWSConfiguration, AWSUser, AWSRegion, AWSAccessKeyId, AWSSecretAccessKey, AWSProfileConfig as AWSLocalUserConfig, AWSAPIKeyUserConfig as AWSAPIKeyUser, AWSEnvironmentVariableUserConfig, AWSTerraformState, RemoteState, };
6
+ import { AWSDeployment } from './types/awsDeployment';
7
+ export type { AWSDeployment, AWSDeploymentRegion, AWSUserName, } from './types/awsDeployment';
8
+ export declare const readDeploymentFromPackageConfig: (deploymentName: string, path?: string) => AWSDeployment;
9
+ export declare const assertTerraformConfig: (user: Name, path?: string) => AWSTerraformState;
10
+ export declare const writeTerraformConfig: (config: AWSTerraformState, path?: string) => void;
11
+ export declare const hasConfig: (path?: string) => boolean;
12
+ export declare const readConfig: (path?: string) => AWSConfiguration;
13
+ export declare const writeConfig: (config: AWSConfiguration, path?: string) => void;
14
+ export declare const createDefaultConfig: () => AWSConfiguration;
15
+ /**
16
+ * Obtains AWS user credentials from config file or environment variables.
17
+ */
18
+ export declare const getAWSUser: (userName: string, configPath?: string) => Promise<AWS.Credentials>;
19
19
  //# sourceMappingURL=infraAws.d.ts.map
@@ -1,112 +1,112 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getAWSUser = exports.createDefaultConfig = exports.writeConfig = exports.readConfig = exports.hasConfig = exports.writeTerraformConfig = exports.assertTerraformConfig = exports.readDeploymentFromPackageConfig = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const utils_config_1 = require("@goldstack/utils-config");
9
- const utils_package_1 = require("@goldstack/utils-package");
10
- const utils_sh_1 = require("@goldstack/utils-sh");
11
- const accountConfigSchema_json_1 = __importDefault(require("./schemas/accountConfigSchema.json"));
12
- const deploymentConfigSchema_json_1 = __importDefault(require("./schemas/deploymentConfigSchema.json"));
13
- const awsTerraformStateSchema_json_1 = __importDefault(require("./schemas/awsTerraformStateSchema.json"));
14
- const awsUserUtils_1 = require("./awsUserUtils");
15
- const readDeploymentFromPackageConfig = (deploymentName, path) => {
16
- const packageConfig = (0, utils_package_1.readPackageConfig)(path);
17
- const deployment = packageConfig.deployments.find((d) => d.name === deploymentName);
18
- if (!deployment) {
19
- throw new Error('Cannot find deployment with name: ' + deploymentName);
20
- }
21
- (0, utils_config_1.validateConfig)(deployment, deploymentConfigSchema_json_1.default, {
22
- errorMessage: `Invalid AWS deployment ${deploymentName}`,
23
- });
24
- return deployment;
25
- };
26
- exports.readDeploymentFromPackageConfig = readDeploymentFromPackageConfig;
27
- const assertTerraformConfig = (user, path) => {
28
- if (!path) {
29
- path = (0, utils_config_1.getAwsTerraformConfigPath)('./../../');
30
- }
31
- let res;
32
- if (fs_1.default.existsSync(path)) {
33
- res = (0, utils_config_1.parseConfig)((0, utils_sh_1.read)(path), awsTerraformStateSchema_json_1.default, {
34
- errorMessage: `Cannot load AWS Terraform configuration from ${path}`,
35
- });
36
- }
37
- else {
38
- res = {
39
- remoteState: [],
40
- };
41
- }
42
- if (!res.remoteState.find((el) => el.user == user)) {
43
- res.remoteState.push({
44
- user: user,
45
- });
46
- }
47
- return res;
48
- };
49
- exports.assertTerraformConfig = assertTerraformConfig;
50
- const writeTerraformConfig = (config, path) => {
51
- if (!path) {
52
- path = (0, utils_config_1.getAwsTerraformConfigPath)('./../../');
53
- }
54
- (0, utils_sh_1.write)(JSON.stringify(config, null, 2), path);
55
- };
56
- exports.writeTerraformConfig = writeTerraformConfig;
57
- const hasConfig = (path) => {
58
- if (!path) {
59
- path = (0, utils_config_1.getAwsConfigPath)('./../../');
60
- }
61
- // otherwise check default config file location
62
- return fs_1.default.existsSync(path);
63
- };
64
- exports.hasConfig = hasConfig;
65
- const readConfig = (path) => {
66
- if (!path) {
67
- path = (0, utils_config_1.getAwsConfigPath)('./../../');
68
- }
69
- // otherwise check default config file location
70
- if (!fs_1.default.existsSync(path)) {
71
- throw new Error(`AWS configuration file does not exist: ${path}.`);
72
- }
73
- return (0, utils_config_1.parseConfig)((0, utils_sh_1.read)(path), accountConfigSchema_json_1.default, {
74
- errorMessage: `Cannot load AWS configuration from ${path}`,
75
- });
76
- };
77
- exports.readConfig = readConfig;
78
- const writeConfig = (config, path) => {
79
- if (!path) {
80
- path = (0, utils_config_1.getAwsConfigPath)('./../../');
81
- }
82
- (0, utils_sh_1.write)(JSON.stringify(config, null, 2), path);
83
- };
84
- exports.writeConfig = writeConfig;
85
- const createDefaultConfig = () => {
86
- return {
87
- users: [],
88
- };
89
- };
90
- exports.createDefaultConfig = createDefaultConfig;
91
- /**
92
- * Obtains AWS user credentials from config file or environment variables.
93
- */
94
- const getAWSUser = async (userName, configPath) => {
95
- // Load from ECS environment if running in ECS
96
- if (process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) {
97
- return await (0, awsUserUtils_1.getAWSUserFromContainerEnvironment)();
98
- }
99
- // Load credentials from environment variables if available
100
- if (process.env.AWS_ACCESS_KEY_ID) {
101
- return await (0, awsUserUtils_1.getAWSUserFromEnvironmentVariables)();
102
- }
103
- // Try loading default local user if no config file provided
104
- if (!(0, exports.hasConfig)(configPath)) {
105
- return await (0, awsUserUtils_1.getAWSUserFromDefaultLocalProfile)();
106
- }
107
- // Load users as configured in Goldstack configuration
108
- const config = (0, exports.readConfig)(configPath);
109
- return await (0, awsUserUtils_1.getAWSUserFromGoldstackConfig)(config, userName);
110
- };
111
- exports.getAWSUser = getAWSUser;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getAWSUser = exports.createDefaultConfig = exports.writeConfig = exports.readConfig = exports.hasConfig = exports.writeTerraformConfig = exports.assertTerraformConfig = exports.readDeploymentFromPackageConfig = void 0;
7
+ const fs_1 = __importDefault(require("fs"));
8
+ const utils_config_1 = require("@goldstack/utils-config");
9
+ const utils_package_1 = require("@goldstack/utils-package");
10
+ const utils_sh_1 = require("@goldstack/utils-sh");
11
+ const accountConfigSchema_json_1 = __importDefault(require("./schemas/accountConfigSchema.json"));
12
+ const deploymentConfigSchema_json_1 = __importDefault(require("./schemas/deploymentConfigSchema.json"));
13
+ const awsTerraformStateSchema_json_1 = __importDefault(require("./schemas/awsTerraformStateSchema.json"));
14
+ const awsUserUtils_1 = require("./awsUserUtils");
15
+ const readDeploymentFromPackageConfig = (deploymentName, path) => {
16
+ const packageConfig = (0, utils_package_1.readPackageConfig)(path);
17
+ const deployment = packageConfig.deployments.find((d) => d.name === deploymentName);
18
+ if (!deployment) {
19
+ throw new Error('Cannot find deployment with name: ' + deploymentName);
20
+ }
21
+ (0, utils_config_1.validateConfig)(deployment, deploymentConfigSchema_json_1.default, {
22
+ errorMessage: `Invalid AWS deployment ${deploymentName}`,
23
+ });
24
+ return deployment;
25
+ };
26
+ exports.readDeploymentFromPackageConfig = readDeploymentFromPackageConfig;
27
+ const assertTerraformConfig = (user, path) => {
28
+ if (!path) {
29
+ path = (0, utils_config_1.getAwsTerraformConfigPath)('./../../');
30
+ }
31
+ let res;
32
+ if (fs_1.default.existsSync(path)) {
33
+ res = (0, utils_config_1.parseConfig)((0, utils_sh_1.read)(path), awsTerraformStateSchema_json_1.default, {
34
+ errorMessage: `Cannot load AWS Terraform configuration from ${path}`,
35
+ });
36
+ }
37
+ else {
38
+ res = {
39
+ remoteState: [],
40
+ };
41
+ }
42
+ if (!res.remoteState.find((el) => el.user == user)) {
43
+ res.remoteState.push({
44
+ user: user,
45
+ });
46
+ }
47
+ return res;
48
+ };
49
+ exports.assertTerraformConfig = assertTerraformConfig;
50
+ const writeTerraformConfig = (config, path) => {
51
+ if (!path) {
52
+ path = (0, utils_config_1.getAwsTerraformConfigPath)('./../../');
53
+ }
54
+ (0, utils_sh_1.write)(JSON.stringify(config, null, 2), path);
55
+ };
56
+ exports.writeTerraformConfig = writeTerraformConfig;
57
+ const hasConfig = (path) => {
58
+ if (!path) {
59
+ path = (0, utils_config_1.getAwsConfigPath)('./../../');
60
+ }
61
+ // otherwise check default config file location
62
+ return fs_1.default.existsSync(path);
63
+ };
64
+ exports.hasConfig = hasConfig;
65
+ const readConfig = (path) => {
66
+ if (!path) {
67
+ path = (0, utils_config_1.getAwsConfigPath)('./../../');
68
+ }
69
+ // otherwise check default config file location
70
+ if (!fs_1.default.existsSync(path)) {
71
+ throw new Error(`AWS configuration file does not exist: ${path}.`);
72
+ }
73
+ return (0, utils_config_1.parseConfig)((0, utils_sh_1.read)(path), accountConfigSchema_json_1.default, {
74
+ errorMessage: `Cannot load AWS configuration from ${path}`,
75
+ });
76
+ };
77
+ exports.readConfig = readConfig;
78
+ const writeConfig = (config, path) => {
79
+ if (!path) {
80
+ path = (0, utils_config_1.getAwsConfigPath)('./../../');
81
+ }
82
+ (0, utils_sh_1.write)(JSON.stringify(config, null, 2), path);
83
+ };
84
+ exports.writeConfig = writeConfig;
85
+ const createDefaultConfig = () => {
86
+ return {
87
+ users: [],
88
+ };
89
+ };
90
+ exports.createDefaultConfig = createDefaultConfig;
91
+ /**
92
+ * Obtains AWS user credentials from config file or environment variables.
93
+ */
94
+ const getAWSUser = async (userName, configPath) => {
95
+ // Load from ECS environment if running in ECS
96
+ if (process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) {
97
+ return await (0, awsUserUtils_1.getAWSUserFromContainerEnvironment)();
98
+ }
99
+ // Load credentials from environment variables if available
100
+ if (process.env.AWS_ACCESS_KEY_ID) {
101
+ return await (0, awsUserUtils_1.getAWSUserFromEnvironmentVariables)();
102
+ }
103
+ // Try loading default local user if no config file provided
104
+ if (!(0, exports.hasConfig)(configPath)) {
105
+ return await (0, awsUserUtils_1.getAWSUserFromDefaultLocalProfile)();
106
+ }
107
+ // Load users as configured in Goldstack configuration
108
+ const config = (0, exports.readConfig)(configPath);
109
+ return await (0, awsUserUtils_1.getAWSUserFromGoldstackConfig)(config, userName);
110
+ };
111
+ exports.getAWSUser = getAWSUser;
112
112
  //# sourceMappingURL=infraAws.js.map