import { ClientSecretCredential } from "@azure/identity" ; import * as msRestNodeAuth from "@azure/ms-rest-nodeauth" ; import { Config } from "../../config" ; import { logger } from "../../logger" ; import { AzureAccessOpts } from "../../types" ; import { build as buildError } from "../../lib/errorBuilder" ; import { errorStatusCode } from "../errorStatusCode" ; const verifyConfigDefined = ( servicePrincipalId ? , servicePrincipalPassword ? , tenantId ? ) : boolean => { if ( servicePrincipalId && servicePrincipalPassword && tenantId ) { return true ; } logger . error ( `Configuration is missing required fields tenant_id, service_principal_id and service_principal_secret. Please run the init command` ) ; return false ; } ; param opts export const getCredentials = async ( opts = { } ) : Promise < ClientSecretCredential | undefined > => { let servicePrincipalId = opts . servicePrincipalId ; let servicePrincipalPassword = opts . servicePrincipalPassword ; let tenantId = opts . tenantId ; const config = Config ( ) ; const azure = config && config . introspection ? config . introspection . azure : undefined ; if ( azure ) { servicePrincipalId = servicePrincipalId || azure . service_principal_id ; servicePrincipalPassword = servicePrincipalPassword || azure . service_principal_secret ; tenantId = tenantId || azure . tenant_id ; } if ( ! verifyConfigDefined ( servicePrincipalId , servicePrincipalPassword , tenantId ) ) { return undefined ; } return new ClientSecretCredential ( tenantId ! , servicePrincipalId ! , servicePrincipalPassword ! ) ; } ; param opts export const getManagementCredentials = async ( opts = { } ) : Promise < msRestNodeAuth . ApplicationTokenCredentials | undefined > => { const conf = Config ( ) ; let servicePrincipalId = opts . servicePrincipalId ; let servicePrincipalPassword = opts . servicePrincipalPassword ; let tenantId = opts . tenantId ; if ( conf && conf . introspection && conf . introspection . azure ) { const azure = conf . introspection . azure ; servicePrincipalId = servicePrincipalId || azure . service_principal_id ; servicePrincipalPassword = servicePrincipalPassword || azure . service_principal_secret ; tenantId = tenantId || azure . tenant_id ; } if ( ! verifyConfigDefined ( servicePrincipalId , servicePrincipalPassword , tenantId ) ) { return undefined ; } try { return await msRestNodeAuth . loginWithServicePrincipalSecret ( servicePrincipalId ! , servicePrincipalPassword ! , tenantId ! ) ; } catch ( err ) { throw buildError ( errorStatusCode . AZURE_CLIENT , "azure-client-auth-sp-err" , err ) ; } } ;