/**
 * 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.
 */ import { AuthenticationDetailsProvider , SimpleAuthenticationDetailsProvider , RegionProvider } from "./auth" ; import { checkNotNull } from "../utils" ; import { ConfigFileReader , ConfigFile , ConfigAccumulator } from "../config-file-reader" ; import { readFileSync } from "fs" ; import { Region } from "../region" ; import { Realm } from "../realm" ; export class ConfigFileAuthenticationDetailsProvider implements AuthenticationDetailsProvider , RegionProvider { private delegate : SimpleAuthenticationDetailsProvider ; /**
     * Creates a new instance using the config file at the default location,
     * see  ConfigFileReader .
     *
     *  param profile param configurationFilePath throws constructor ( configurationFilePath ? , profile ? ) { const pfl = profile || ConfigFileReader . DEFAULT_PROFILE_NAME ; if ( configurationFilePath ) { this . delegate = this . createConfigFileAuth ( ConfigFileReader . parseFileFromPath ( configurationFilePath , pfl ) ) ; } else { this . delegate = this . createConfigFileAuth ( ConfigFileReader . parseDefault ( pfl ) ) ; } } param regionId param region return retrieveRegionFromRegionId ( regionId ) { let region ; try { region = Region . fromRegionId ( regionId ) ; if ( ! region ) { console . warn ( `Found regionId ${ regionId } in config file, but not supported by this version of the SDK` ) ; const fallbackSecondLevelDomain = process . env [ "OCI_DEFAULT_REALM" ] ; if ( fallbackSecondLevelDomain ) { console . warn ( `Falling back to using second level domain: ${ fallbackSecondLevelDomain }` ) ; const unknownRealm = Realm . register ( "unknown" , fallbackSecondLevelDomain ) ; region = Region . register ( regionId , unknownRealm ) ; } else { console . warn ( `Falling back to using OC1 realm.` ) ; region = Region . register ( regionId , Realm . OC1 ) ; } } return region ; } catch ( e ) { throw new Error ( `Error from retrying to retrieve region from regionId: ${ e }` ) ; } } createConfigFileAuth ( file ) { const authType = file . get ( "authentication_type" ) ; const tenantId = checkNotNull ( file . get ( "tenancy" ) , "missing tenancy in config" ) ; const delegationTokenPath = file . get ( "delegation_token_file" ) ; let delegationToken = "" ; let region = null ; const regionEnvVar = process . env . OCI_REGION ; let regionId = file . get ( "region" ) || regionEnvVar ; if ( regionId ) { region = this . retrieveRegionFromRegionId ( regionId ! ) ; } else { throw Error ( "Region not specified in Config file or OCI_REGION env variable. Can not proceed without setting a region." ) ; } if ( delegationTokenPath ) { delegationToken = readFileSync ( delegationTokenPath , "utf8" ) . replace ( /\n/g , "" ) ; } if ( authType ) { return new SimpleAuthenticationDetailsProvider ( tenantId , "" , "" , "" , "" , region , authType , delegationToken ) ; } const fingerprint = checkNotNull ( file . get ( "fingerprint" ) , "missing fingerprint in config" ) ; const user = checkNotNull ( file . get ( "user" ) , "missing user in config" ) ; const pemFilePath = checkNotNull ( file . get ( "key_file" ) , "missing key_file in config" ) ; const passPhrase = file . get ( "pass_phrase" ) ; const privateKey = this . getPvtKey ( ConfigFileReader . expandUserHome ( pemFilePath ) ) ; const profileCredentials = file . profileCredentials ; return new SimpleAuthenticationDetailsProvider ( tenantId , user , fingerprint , privateKey , passPhrase , region , undefined , undefined , profileCredentials ) ; } getPvtKey ( filePath ) { try { const key = readFileSync ( filePath , "utf8" ) ; return key ; } catch ( e ) { throw "Failed to read private key from file path." ; } } /**
     * Get the key id to sign the http request.
     */ public async getKeyId ( ) { return await this . delegate . getKeyId ( ) ; } /**
     * Get the private key to sign the http request.
     */ public getPrivateKey ( ) { return this . delegate . getPrivateKey ( ) ; } /**
     * Get the passphrase of the private key to sign the http request.
     */ public getPassphrase ( ) { return this . delegate . getPassphrase ( ) ; } /**
     * Get the Tenancy
     */ public getTenantId ( ) { return this . delegate . getTenantId ( ) ; } /**
     * Get the user
     */ public getUser ( ) { return this . delegate . getUser ( ) ; } /**
     * Get the fingerprint
     */ public getFingerprint ( ) { return this . delegate . getFingerprint ( ) ; } /**
     * Get the region
     */ public getRegion ( ) { return this . delegate . getRegion ( ) ; } /**
     * Set the region
     */ public setRegion ( regionId ) { this . delegate . setRegion ( this . retrieveRegionFromRegionId ( regionId ) ) ; } /**
     * Get the authType
     */ public getAuthType ( ) { return this . delegate . getAuthType ( ) ; } /**
     * Set the provider
     */ public setProvider ( provider ) { this . delegate . setProvider ( provider ) ; } /**
     * Get the provider
     */ public getProvider ( ) { return this . delegate . getProvider ( ) ; } public getDelegationToken ( ) { return this . delegate . getDelegationToken ( ) ; } public getProfileCredentials ( ) { return this . delegate . getProfileCredentials ( ) ; } }