@adpt/cloud
Version:
AdaptJS cloud component library
95 lines • 3.27 kB
JavaScript
;
/*
* Copyright 2018-2019 Unbounded Systems, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs-extra"));
const path = tslib_1.__importStar(require("path"));
const core_1 = tslib_1.__importStar(require("@adpt/core"));
/** @beta */
function awsCredentialsContext(defaultCreds) {
return core_1.createContext(defaultCreds);
}
exports.awsCredentialsContext = awsCredentialsContext;
/** @beta */
exports.awsDefaultCredentialsContext = awsCredentialsContext({
awsAccessKeyId: "",
awsSecretAccessKey: "",
awsRegion: "",
});
/** @beta */
function withCredentials(
// tslint:disable-next-line:variable-name
Wrapped, Ctx = exports.awsDefaultCredentialsContext) {
return (props) => {
const _a = props, { children, handle } = _a, rest = tslib_1.__rest(_a, ["children", "handle"]);
return (core_1.default.createElement(Ctx.Consumer, { key: props.key }, (awsCredentials) => (core_1.default.createElement(Wrapped, Object.assign({ awsCredentials: awsCredentials }, rest), children))));
};
}
exports.withCredentials = withCredentials;
/** @beta */
async function loadAwsCreds(options = {}) {
let { credsFile } = options;
let creds = loadEnvCreds();
if (creds)
return creds;
if (credsFile == null) {
const home = process.env.HOME;
if (home == null)
throw new Error(`Unable to get home directory for AWS credentials`);
credsFile = path.join(home, ".adaptAwsCreds");
}
creds = await loadFileCreds(credsFile);
if (creds == null)
throw new Error(`Unable to find AWS credentials`);
return creds;
}
exports.loadAwsCreds = loadAwsCreds;
function isAwsCredentials(val) {
function valid(obj, prop) {
return typeof obj[prop] === "string";
}
if (val == null || typeof val !== "object")
return false;
return (valid(val, "awsAccessKeyId") &&
valid(val, "awsSecretAccessKey") &&
valid(val, "awsRegion"));
}
async function loadFileCreds(credsFile) {
try {
const creds = await fs.readJson(credsFile);
if (!isAwsCredentials(creds))
return null;
return creds;
}
catch (err) {
return null;
}
}
function loadEnvCreds() {
const awsAccessKeyId = process.env.AWS_ACCESS_KEY_ID;
const awsSecretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
const awsRegion = process.env.AWS_DEFAULT_REGION;
const creds = {
awsAccessKeyId,
awsSecretAccessKey,
awsRegion,
};
if (!isAwsCredentials(creds))
return null;
return creds;
}
//# sourceMappingURL=credentials.js.map