【问题标题】:Authenticate to Azure with certificate from Linux使用来自 Linux 的证书向 Azure 进行身份验证
【发布时间】:2019-11-08 09:40:06
【问题描述】:

我正在尝试从带有 Az 模块的 Powershell Core 脚本登录到 Azure。 这需要使用上传到 Azure 的自签名证书。

我尝试使用以下方法创建证书:

openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -keyout /etc/ssl/private/key.pem -out /etc/ssl/private/cert.pem -subj "/C=LV/ST=Some-State/L=LV/O=IT/OU=IT"

并使用指纹登录,但 Powershell 给了我这个错误:

Connect-AzAccount : Unix LocalMachine X509Store is limited to the Root and CertificateAuthority stores.

不知道这是什么意思。

问题类似于这个问题https://github.com/Azure/azure-powershell/issues/8658

但不确定如何解释那里的答案。没有证书方面的经验,并且在 Linux 方面的经验有限。

【问题讨论】:

    标签: linux azure powershell x509certificate x509


    【解决方案1】:

    为了回答我自己的问题,我终于有点想通了。步骤:

    #create certs
    openssl req -new -x509 -newkey rsa:2048 -keyout localhost.key -out localhost.cer -days 365 -subj /CN=localhost
    
    #create pfx
    openssl pkcs12 -export -out localhost.pfx -inkey localhost.key -in localhost.cer
    
    #You will need to specify some password for it
    #Now use the generated cer file and import it in your Azure portal, AzureAD->app registrations->your created SP->Certificates and secrets. Can also use powershell to do this.
    
    #import the PFX to your machines cert store
    $StoreName = [System.Security.Cryptography.X509Certificates.StoreName]::My 
    $StoreLocation = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser 
    $Store = [System.Security.Cryptography.X509Certificates.X509Store]::new($StoreName, $StoreLocation) 
    $Flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable 
    $Certificate = [System.Security.Cryptography.X509Certificates.X509Certificate2]::new("path to your pfx","the pfx password you specified on step 2",$Flag) 
    $Store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) 
    $Store.Add($Certificate) 
    $Store.Close() 
    
    $tenantId = 'look in your azure portal' 
    $appId = 'app id of the service principal you created, look in your azure portal'
    $thumbprint = $certificate.thumbprint
    
    Connect-AzAccount -ApplicationId $appId -Tenant $tenantId -CertificateThumbprint $thumbprint
    

    就是这样,您将使用 Powershell Core 从 Linux 计算机或 Docker 以非交互方式自动连接到您的 Azure 租户,并且可以执行您的 SP 角色允许的所有命令。您可以重复使用 PFX 文件,只是第一次是手动的,然后将其托管在某个地方并使用 curl 或类似的脚本加载它。

    注意:我不太了解证书以及所有这些可能产生的安全隐患,使用风险自负。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-17
      • 2023-03-29
      • 2017-03-21
      • 2018-09-30
      • 1970-01-01
      • 2013-03-31
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多