PowerShell 的 Artifactory 基本身份验证抛出 403

发布于 2025-01-11 14:47:21 字数 817 浏览 4 评论 0 原文

我正在尝试从 powershell 向 JFROG 进行身份验证,我用我的令牌进行了尝试,结果出现 403 禁止

  PS C:\Myproject> $myHeaders = @{'X-JFrog-Art-Api' = 'AKCp8sdFTEKF1Y5MDgM3M8RK6bRkKWoX43jWranZvS2U2DE82KFE7243F'}

  PS C:\Myproject> Invoke-WebRequest -Uri "https://org.jfrog.io/" -Method Get -Headers $myHeaders

    Invoke-WebRequest : 403 Forbidden
    nginx
   At line:1 char:1
    + Invoke-WebRequest -Uri "https://fiprod.jfrog.io/" -Method Get -Header ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

知道如何使用 PowerShell 通过令牌向 Artifactory 进行身份验证吗?

I am trying to authenticate to JFROG from powershell i tried it with my token and its giving 403 forbidden

  PS C:\Myproject> $myHeaders = @{'X-JFrog-Art-Api' = 'AKCp8sdFTEKF1Y5MDgM3M8RK6bRkKWoX43jWranZvS2U2DE82KFE7243F'}

  PS C:\Myproject> Invoke-WebRequest -Uri "https://org.jfrog.io/" -Method Get -Headers $myHeaders

    Invoke-WebRequest : 403 Forbidden
    nginx
   At line:1 char:1
    + Invoke-WebRequest -Uri "https://fiprod.jfrog.io/" -Method Get -Header ...
  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Any idea on how to authenticate to Artifactory via a token using PowerShell ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沦落红尘 2025-01-18 14:47:21

不确定这是否仍然需要,但也许会帮助其他人。在大多数情况下,基本用户无法创建 API 密钥,因此您只能选择仅使用需要与您的用户名关联的令牌 ID。

基于官方文档 https://jfrog.com/knowledge-base/artifactory-how-to-download-or-upload-a-file-from-to-artifactory-using-powershell/ ,您必须创建您的标头如下:

-Headers @{ Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("USERNAME:PASSWORD")) }

其中用户名是您的 JFrog 工件用户名,密码是您的身份验证令牌。

作为一个好的做法,这样您就不会在脚本中共享敏感信息,您可以在环境变量中添加用户和令牌,并且您可以从脚本中读取它们(假设将它们作为运行脚本是可能的)。

要使用这种方法,您可以执行以下操作:

$artifactoryUserName = [System.Environment]::GetEnvironmentVariable('ARTIFACTORY_USER');
$artifactoryPassword = [System.Environment]::GetEnvironmentVariable('ARTIFACTORY_PASSWORD');
$artifactoryCredentials = $artifactoryUserName + ":" + $artifactoryPassword;

其中 ARTIFACTORY_USER 和 ARTIFACTORY_PASSWORD 是保存 JFrog 工件凭证的环境变量,而 $artifactoryCredentials 变量只是创建身份验证标头格式所需的身份验证字符串,您将其合并到标头中像这样:

-Headers @{ Authorization = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($artifactoryCredentials)) }

或者您可以将整个身份验证标头保存在另一个变量中。

Not sure if this is still needed but maybe will help others. In most cases basic users cannot create an API key, leaving you with the only option of using only the token ID which needs to be used in correlation with your username.

Based on the official documentation https://jfrog.com/knowledge-base/artifactory-how-to-download-or-upload-a-file-from-to-artifactory-using-powershell/ , you have to create your header as follows:

-Headers @{ Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("USERNAME:PASSWORD")) }

where username is your JFrog artifactory username and the password is your authentication token.

As a good practice so you do not share sensitive information in your script you could add the user and token in your environment variables, and you can just read them from there in your script (assuming that having them as environment variables on your machines running the script is possible).

To use this approach you could do something like this:

$artifactoryUserName = [System.Environment]::GetEnvironmentVariable('ARTIFACTORY_USER');
$artifactoryPassword = [System.Environment]::GetEnvironmentVariable('ARTIFACTORY_PASSWORD');
$artifactoryCredentials = $artifactoryUserName + ":" + $artifactoryPassword;

where ARTIFACTORY_USER and ARTIFACTORY_PASSWORD are the environment variables holding the credentials for your JFrog artifactory and the $artifactoryCredentials variable is just creating the authentication string required by the authentication header format which you will incorporate in the header like this:

-Headers @{ Authorization = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($artifactoryCredentials)) }

or you can hold the entire authentication header in another variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文