oci-common
Advanced tools
Comparing version 1.5.4 to 1.5.5
@@ -16,2 +16,3 @@ /** | ||
import { LOG } from "./lib/log"; | ||
import Constants from "./lib/constants"; | ||
import { DelayStrategy, TerminationStrategy, ExponentialBackoffDelayStrategy, MaxTimeTerminationStrategy, genericWaiter, genericTerminalConditionWaiter, WaiterConfiguration, MaxAttemptsTerminationStrategy, FixedTimeDelayStrategy } from "./lib/waiter"; | ||
@@ -46,2 +47,2 @@ import { GenericRetrier, RetryConfiguration } from "./lib/retrier"; | ||
export import Range = range.Range; | ||
export { Region, Realm, EndpointBuilder, DelayStrategy, TerminationStrategy, ExponentialBackoffDelayStrategy, MaxTimeTerminationStrategy, genericWaiter, genericTerminalConditionWaiter, WaiterConfiguration, RequireOnlyOne, AuthParams, paginateRecords, paginatedResponsesWithLimit, paginatedRecordsWithLimit, genericPaginateRecords, paginateResponses, genericPaginateResponses, Method, composeRequest, composeResponse, HttpRequest, ConfigFileAuthenticationDetailsProvider, ConfigFileReader, InstancePrincipalsAuthenticationDetailsProviderBuilder, ResourcePrincipalAuthenticationDetailsProvider, LOG, GenericRetrier, FixedTimeDelayStrategy, MaxAttemptsTerminationStrategy, RetryConfiguration, BaseRequest, ClientConfiguration }; | ||
export { Region, Realm, EndpointBuilder, DelayStrategy, TerminationStrategy, ExponentialBackoffDelayStrategy, MaxTimeTerminationStrategy, genericWaiter, genericTerminalConditionWaiter, WaiterConfiguration, RequireOnlyOne, AuthParams, paginateRecords, paginatedResponsesWithLimit, paginatedRecordsWithLimit, genericPaginateRecords, paginateResponses, genericPaginateResponses, Method, composeRequest, composeResponse, HttpRequest, ConfigFileAuthenticationDetailsProvider, ConfigFileReader, InstancePrincipalsAuthenticationDetailsProviderBuilder, ResourcePrincipalAuthenticationDetailsProvider, LOG, GenericRetrier, FixedTimeDelayStrategy, MaxAttemptsTerminationStrategy, RetryConfiguration, BaseRequest, ClientConfiguration, Constants }; |
@@ -32,2 +32,4 @@ "use strict"; | ||
exports.LOG = log_1.LOG; | ||
const constants_1 = __importDefault(require("./lib/constants")); | ||
exports.Constants = constants_1.default; | ||
const waiter_1 = require("./lib/waiter"); | ||
@@ -34,0 +36,0 @@ exports.ExponentialBackoffDelayStrategy = waiter_1.ExponentialBackoffDelayStrategy; |
@@ -19,2 +19,13 @@ /** | ||
private static KNOWN_REGIONS; | ||
private static hasCalledForImds; | ||
private static hasUsedConfigFile; | ||
private static hasUsedEnvVar; | ||
private static imdsRegionMetadata; | ||
private static REGIONS_CONFIG_FILE_PATH; | ||
private static OCI_REGION_METADATA_ENV_VAR; | ||
private static IMDS_BASE_URL; | ||
private static METADATA_AUTH_HEADERS; | ||
private static AUTHORIZATION; | ||
private static CONTENT_TYPE_HEADER; | ||
private static CONTENT_TYPE_HEADER_VALUE; | ||
private constructor(); | ||
@@ -49,3 +60,7 @@ static AP_CHUNCHEON_1: Region; | ||
static fromRegionId(regionId: string): Region; | ||
static register(regionId: string, realm: Realm): Region; | ||
private static addRegionsFromConfigFile; | ||
private static addRegionFromEnvVar; | ||
private static addRegionFromImds; | ||
static enableInstanceMetadata(): Promise<void>; | ||
static register(regionId: string, realm: Realm, regionCode?: string): Region; | ||
/** | ||
@@ -52,0 +67,0 @@ * Function to get regionId based regionStr: regionStr can be a short code or regionId |
@@ -6,9 +6,24 @@ "use strict"; | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const realm_1 = require("./realm"); | ||
const config_file_reader_1 = require("./config-file-reader"); | ||
const fs_1 = require("fs"); | ||
const region_metadata_schema_1 = require("./region-metadata-schema"); | ||
const http_1 = require("./http"); | ||
class Region { | ||
constructor(regionId, realm) { | ||
constructor(regionId, realm, regionCode) { | ||
this._realm = realm; | ||
this._regionId = regionId; | ||
Region.KNOWN_REGIONS.set(regionId, this); | ||
if (regionCode) | ||
Region.REGIONS_SHORT_NAMES[regionCode] = regionId; | ||
} | ||
@@ -22,8 +37,104 @@ get realm() { | ||
static fromRegionId(regionId) { | ||
/* | ||
* load provided region from already registered regions if it exists | ||
* else load provided region from region configuration file if it exists | ||
* else load provided region from region metadeta environment variable | ||
* else if instance metadeta service (IMDS) has been opted in, try loading region from IMDS | ||
*/ | ||
if (!regionId) | ||
throw Error("RegionId can not be empty or undefined"); | ||
regionId = regionId.trim().toLocaleLowerCase("en-US"); | ||
return Region.KNOWN_REGIONS.get(regionId); | ||
let foundRegion = Region.KNOWN_REGIONS.get(regionId); | ||
if (!foundRegion) { | ||
Region.addRegionsFromConfigFile(); | ||
foundRegion = Region.KNOWN_REGIONS.get(regionId); | ||
} | ||
if (!foundRegion) { | ||
Region.addRegionFromEnvVar(); | ||
foundRegion = Region.KNOWN_REGIONS.get(regionId); | ||
} | ||
if (!foundRegion && Region.hasCalledForImds) { | ||
Region.addRegionFromImds(); | ||
foundRegion = Region.KNOWN_REGIONS.get(regionId); | ||
} | ||
return foundRegion; | ||
} | ||
static register(regionId, realm) { | ||
// Adds regions from the config file | ||
static addRegionsFromConfigFile() { | ||
if (!Region.hasUsedConfigFile) { | ||
Region.hasUsedConfigFile = true; | ||
let expandedRegionConfigFilePath = config_file_reader_1.ConfigFileReader.expandUserHome(Region.REGIONS_CONFIG_FILE_PATH); | ||
if (config_file_reader_1.ConfigFileReader.fileExists(expandedRegionConfigFilePath)) { | ||
try { | ||
const fileContent = fs_1.readFileSync(expandedRegionConfigFilePath, "utf8"); | ||
const regionMetadata = JSON.parse(fileContent); | ||
if (regionMetadata && regionMetadata.length > 0 && Array.isArray(regionMetadata)) { | ||
regionMetadata.map(metadata => { | ||
if (region_metadata_schema_1.RegionMetadataSchema.isValidSchema(metadata)) { | ||
Region.register(metadata.regionIdentifier, realm_1.Realm.register(metadata.realmKey, metadata.realmDomainComponent), metadata.regionKey); | ||
} | ||
}); | ||
} | ||
} | ||
catch (error) { | ||
console.log("error reading or parsing region config file"); | ||
} | ||
} | ||
} | ||
} | ||
// Adds region from the environment variable | ||
static addRegionFromEnvVar() { | ||
if (!Region.hasUsedEnvVar) { | ||
Region.hasUsedEnvVar = true; | ||
const envVarRegionMetadata = process.env[Region.OCI_REGION_METADATA_ENV_VAR]; | ||
if (envVarRegionMetadata) { | ||
try { | ||
const regionMetadata = JSON.parse(envVarRegionMetadata); | ||
if (region_metadata_schema_1.RegionMetadataSchema.isValidSchema(regionMetadata)) { | ||
Region.register(regionMetadata.regionIdentifier, realm_1.Realm.register(regionMetadata.realmKey, regionMetadata.realmDomainComponent), regionMetadata.regionKey); | ||
} | ||
} | ||
catch (error) { | ||
console.log("error reading or parsing region metadata env var config file"); | ||
} | ||
} | ||
} | ||
} | ||
// Add region from the Instance Metadata Service | ||
static addRegionFromImds() { | ||
if (Region.imdsRegionMetadata) { | ||
Region.register(Region.imdsRegionMetadata.regionIdentifier, realm_1.Realm.register(Region.imdsRegionMetadata.realmKey, Region.imdsRegionMetadata.realmDomainComponent), Region.imdsRegionMetadata.regionKey); | ||
Region.imdsRegionMetadata = undefined; | ||
} | ||
} | ||
/* | ||
* Enable instance metadata lookup for region info | ||
*/ | ||
static enableInstanceMetadata() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!Region.hasCalledForImds) { | ||
Region.hasCalledForImds = true; | ||
try { | ||
const url = Region.IMDS_BASE_URL + "instance/regionInfo/"; | ||
let headers = new Headers(); | ||
headers.append(Region.CONTENT_TYPE_HEADER, Region.CONTENT_TYPE_HEADER_VALUE); | ||
headers.append(Region.AUTHORIZATION, Region.METADATA_AUTH_HEADERS); | ||
const httpClient = new http_1.FetchHttpClient(null); | ||
const response = yield httpClient.send({ | ||
uri: url, | ||
method: "GET", | ||
headers: headers | ||
}); | ||
const regionMetadata = (yield response.json()); | ||
if (region_metadata_schema_1.RegionMetadataSchema.isValidSchema(regionMetadata)) { | ||
Region.imdsRegionMetadata = regionMetadata; | ||
} | ||
} | ||
catch (error) { | ||
console.log("Unable to retrieve region metadata from instance metadata service, reason :" + error); | ||
} | ||
} | ||
}); | ||
} | ||
static register(regionId, realm, regionCode) { | ||
if (!regionId) | ||
@@ -43,3 +154,6 @@ throw Error("RegionId can not be empty or undefined"); | ||
} | ||
return new Region(regionId, realm); | ||
if (regionCode) { | ||
regionCode = regionCode.trim().toLocaleLowerCase("en-US"); | ||
} | ||
return new Region(regionId, realm, regionCode); | ||
} | ||
@@ -54,5 +168,19 @@ /** | ||
regionStr = regionStr.toLocaleLowerCase(); | ||
return Region.REGIONS_SHORT_NAMES[regionStr] | ||
? Region.REGIONS_SHORT_NAMES[regionStr] | ||
: regionStr; | ||
// If region short code is not found in the SDK, add regions from the regions config file | ||
let foundRegionId = Region.REGIONS_SHORT_NAMES[regionStr]; | ||
if (!foundRegionId) { | ||
Region.addRegionsFromConfigFile(); | ||
foundRegionId = Region.REGIONS_SHORT_NAMES[regionStr]; | ||
} | ||
// else add region from environment variable, and then check for short code | ||
if (!foundRegionId) { | ||
Region.addRegionFromEnvVar(); | ||
foundRegionId = Region.REGIONS_SHORT_NAMES[regionStr]; | ||
} | ||
// else add region from IMDS if it has been opted in, and then check for short code | ||
if (!foundRegionId && Region.hasCalledForImds) { | ||
Region.addRegionFromImds(); | ||
foundRegionId = Region.REGIONS_SHORT_NAMES[regionStr]; | ||
} | ||
return foundRegionId ? foundRegionId : regionStr; | ||
} | ||
@@ -91,2 +219,12 @@ } | ||
Region.KNOWN_REGIONS = new Map(); | ||
Region.hasCalledForImds = false; | ||
Region.hasUsedConfigFile = false; | ||
Region.hasUsedEnvVar = false; | ||
Region.REGIONS_CONFIG_FILE_PATH = "~/.oci/regions-config.json"; | ||
Region.OCI_REGION_METADATA_ENV_VAR = "OCI_REGION_METADATA"; | ||
Region.IMDS_BASE_URL = "http://169.254.169.254/opc/v2/"; | ||
Region.METADATA_AUTH_HEADERS = "Bearer Oracle"; | ||
Region.AUTHORIZATION = "Authorization"; | ||
Region.CONTENT_TYPE_HEADER = "Content-Type"; | ||
Region.CONTENT_TYPE_HEADER_VALUE = "application/json"; | ||
// OC1 | ||
@@ -93,0 +231,0 @@ Region.AP_CHUNCHEON_1 = Region.register("ap-chuncheon-1", realm_1.Realm.OC1); |
{ | ||
"name": "oci-common", | ||
"version": "1.5.4", | ||
"version": "1.5.5", | ||
"description": "OCI Common module for NodeJS", | ||
@@ -5,0 +5,0 @@ "repository": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
407933
158
4935
13