/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ /**
 * This constructs a default implementation of the  ResourcePrincipalAuthenticationDetailsProvider , constructed
 * in accordance with the following environment variable settings:
 * <ul>
 *
 * <li>{@code OCI_RESOURCE_PRINCIPAL_VERSION}:
 * <p>permitted values are "2.2"</p>
 * </li>
 *
 * <li>{@code OCI_RESOURCE_PRINCIPAL_RPST}:
 * <p>If this is an absolute path, then the filesystem-supplied resource principal session token will be retrieved from
 *   that location. This mode supports token refresh (if the environment replaces the RPST in the filesystem).</p>
 * <p>Otherwise, the environment variable is taken to hold the raw value of an RPST.
 *   Under these circumstances, the RPST cannot be refreshed; consequently, this mode is only usable for short-lived
 *   executables.</p>
 * </li>
 * <li>{@code OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM}:
 * <p>If this is an absolute path, then the filesystem-supplied private key will be retrieved from that location. As
 *   with the OCI_RESOURCE_PRINCIPAL_RPST, this mode supports token refresh if the environment can update the file
 *   contents.</p>
 * <p>Otherwise, the value is interpreted as the direct injection of a private key. The same considerations as to the
 *   lifetime of this value apply when directly injecting a key.</p>
 * </li>
 * <li>{@code OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM_PASSPHRASE}:
 * <p>This is optional. If set, it contains either the location (as an absolute path) or the value of the passphrase
 *   associated with the private key.</p>
 * </li>
 * <li>{@code OCI_RESOURCE_PRINCIPAL_REGION}:
 * <p>If set, this holds the canonical form of the local region. This is intended to enable executables to locate their
 *   "local" OCI service endpoints.</p>
 * </li>
 * </ul>
  import path from "path" ; import { Region } from "../region" ; import { RegionProvider } from "./auth" ; import FederationClient from "./models/federation-client" ; import FixedContentKeySupplier from "./fixed-content-key-supplier" ; import FileBasedKeySupplier from "./file-based-key-supplier" ; import RefreshableOnNotAuthenticatedProvider from "./models/refreshable-on-not-authenticaticated-provider" ; import AbstractRequestingAuthenticationDetailsProvider from "./abstract-requesting-authentication-detail-provider" ; import SessionKeySupplier from "./models/session-key-supplier" ; import FileBasedResourcePrincipalFederationClient from "./file-based-resource-principal-federation-client" ; import FixedContentResourcePrincipalFederationClient from "./fixed-content-resource-principal-federation-client" ; export default class ResourcePrincipalAuthenticationDetailsProvider extends AbstractRequestingAuthenticationDetailsProvider implements RegionProvider , RefreshableOnNotAuthenticatedProvider < String > { static OCI_RESOURCE_PRINCIPAL_VERSION = "OCI_RESOURCE_PRINCIPAL_VERSION" ; static RP_VERSION_2_2 = "2.2" ; static OCI_RESOURCE_PRINCIPAL_RPST = "OCI_RESOURCE_PRINCIPAL_RPST" ; static OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM = "OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM" ; static OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM_PASSPHRASE = "OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM_PASSPHRASE" ; static OCI_RESOURCE_PRINCIPAL_REGION_ENV_VAR_NAME = "OCI_RESOURCE_PRINCIPAL_REGION" ; constructor ( protected federationClient , protected sessionKeySupplier , protected region ) { super ( federationClient , sessionKeySupplier ) ; } public static ClaimKeys = class ClaimsKey { /**
         * COMPARTMENT_ID is the claim name that the RPST holds for the resource compartment.
         * This can be passed to  {@link #getStringClaim} to retrieve the resource's compartment OCID.
          public static COMPARTMENT_ID_CLAIM_KEY = "res_compartment" ; /**
         * TENANT_ID_CLAIM_KEY is the claim name that the RPST holds for the resource tenancy.
         * This can be passed to  {@link #getStringClaim} to retrieve the resource's tenancy OCID.
          public static TENANT_ID_CLAIM_KEY = "res_tenant" ; } ; public static builder ( ) { return new ResourcePrincipalAuthenticationDetailsProvider . ResourcePrincipalAuthenticationDetailsProviderBuilder ( ) . build ( ) ; } /**
     * Session tokens carry JWT-like claims. Permit the retrieval of the value of those
     * claims from the token.
     * At the least, the token should carry claims for  ClaimKeys and  ClaimKeys param key return public async getStringClaim ( key ) { return await this . federationClient . getStringClaim ( key ) ; } return async refresh ( ) { return await this . federationClient . refreshAndGetSecurityToken ( ) ; } getRegion ( ) { return this . region ! ; } /**
     * Builder for ResourcePrincipalAuthenticationDetailsProvider that understands the V2.2 configuration
     */ public static ResourcePrincipalAuthenticationDetailsProviderBuilder = class ResourcePrincipalAuthenticationDetailsProviderBuilder { constructor ( ) { } public build ( ) { const OciResourcePrincipalVersion = process . env [ ResourcePrincipalAuthenticationDetailsProvider . OCI_RESOURCE_PRINCIPAL_VERSION ] ; if ( ! OciResourcePrincipalVersion ) { throw Error ( "OCI_RESOURCE_PRINCIPAL_VERSION environment variable is missing" ) ; } switch ( OciResourcePrincipalVersion ) { case ResourcePrincipalAuthenticationDetailsProvider . RP_VERSION_2_2 : return ResourcePrincipalAuthenticationDetailsProviderBuilder . build_2_2 ( ) ; default : throw Error ( `OCI_RESOURCE_PRINCIPAL_VERSION environment variable has an unknown value ${ OciResourcePrincipalVersion }` ) ; } } return static build_2_2 ( ) { let federationClient ; let sessionKeySupplier ; let region ; const ociResourcePrincipalPrivateKey = process . env [ ResourcePrincipalAuthenticationDetailsProvider . OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM ] ; const ociResourcePrincipalPassphrase = process . env [ ResourcePrincipalAuthenticationDetailsProvider . OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM_PASSPHRASE ] ; const ociResourcePrincipalRPST = process . env [ ResourcePrincipalAuthenticationDetailsProvider . OCI_RESOURCE_PRINCIPAL_RPST ] ; const ociResourcePrincipalRegion = process . env [ ResourcePrincipalAuthenticationDetailsProvider . OCI_RESOURCE_PRINCIPAL_REGION_ENV_VAR_NAME ] ; if ( ! ociResourcePrincipalPrivateKey ) { throw Error ( "OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM environment variable missing" ) ; } if ( ! ociResourcePrincipalRPST ) { throw Error ( "OCI_RESOURCE_PRINCIPAL_RPST environment variable is missing" ) ; } if ( ! ociResourcePrincipalRegion ) { throw Error ( "OCI_RESOURCE_PRINCIPAL_REGION_ENV_VAR_NAME environment variable missing" ) ; } if ( path . isAbsolute ( ociResourcePrincipalPrivateKey ) ) { if ( ociResourcePrincipalPassphrase && ! path . isAbsolute ( ociResourcePrincipalPassphrase ) ) { throw Error ( "cannot mix path and constant settings for \
          OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM and OCI_RESOURCE_PRINCIPAL_PRIVATE_PEM_PASSPHRASE" ) ; } sessionKeySupplier = new FileBasedKeySupplier ( ociResourcePrincipalPrivateKey , ociResourcePrincipalPassphrase ) ; } else { sessionKeySupplier = new FixedContentKeySupplier ( ociResourcePrincipalPrivateKey , ociResourcePrincipalPassphrase ) ; } if ( path . isAbsolute ( ociResourcePrincipalRPST ) ) { federationClient = new FileBasedResourcePrincipalFederationClient ( sessionKeySupplier , ociResourcePrincipalRPST ) ; } else { federationClient = new FixedContentResourcePrincipalFederationClient ( ociResourcePrincipalRPST , sessionKeySupplier ) ; } const regionId = Region . getRegionIdFromShortCode ( ociResourcePrincipalRegion ) ; region = Region . fromRegionId ( regionId ) ; return new ResourcePrincipalAuthenticationDetailsProvider ( federationClient , sessionKeySupplier , region ) ; } } ; }