notStanley
notStanley

Reputation: 53

how to authenticate Invoke-RestMethod to list artifactory repositories

Trying to get a list of Repositories from an Artifactory Enterprise v6 instance, using PowerShell 5.1 Invoke-RestMethod from Win10 desktop, but cannot see to get it to authenticate.

Seems simple enough, but this

$myCred = Get-Credential notStanley
$lstART = Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Credential $myCred

only returns the items that allow Anonymous access.

If I open a browser and logon to that Artifactory instance, I can then paste the above URI and get the full list of all repositories my account has access to.

Any hints what the $myCred is missing?

Upvotes: 3

Views: 3800

Answers (2)

notStanley
notStanley

Reputation: 53

Thanks Jawad. That got me working with the API (my first try was not formed quite properly). Following your links found a couple other questions (27951561 and 60325084) that helped me get Credential as well. I have gone with Credential to avoid futzing with obfuscating an API Key in the source code.

My base skeleton now looks like:

# get standard PowerShell credential
$myCred  = Get-Credential -Message "just <name> for this server, not '<domain>\<name>'"

# format credential for Artifactory API
$credUser    = $myCred.UserName                                   # extract user name
$credPswd    = $myCred.GetNetworkCredential().password            # extract user password
$credPair    = "${credUser}:${credPswd}"                          # concatenate into BasicAuth format
$credBytes   = [System.Text.Encoding]::ASCII.GetBytes($credPair)  # convert byte values to text
$cred64      = [System.Convert]::ToBase64String($credBytes)       # condense a bit more secure string RFC2045-MIME    
$credAuth    = "Basic $cred64"                                    # intermediate formatting 
$restHeaders = @{ Authorization = $credAuth }                     # initialize web headers

# clear collection array
$cfgSite = @()

# locate server
$lstURL "https://<myserver>/artifactory/api/repositories"

# get list of repositories
$theseRepo = Invoke-RestMethod -Headers $restHeaders -Uri $lstURL 

# collect each configuration
ForEach ($thisRepo in $theseRepo)
   {
   $thisURI  = $lstURL + $thisRepo.key
   $thisCfg  = Invoke-RestMethod -Headers $restHeaders -Uri $thisURI    
   $thisCfg  | Add-Member -NotePropertyName "SITE" -NotePropertyValue "$thisSite"     
   $cfgSite += $thisCfg
   }

# output to file
$cfgAll | Export-Csv .\lstArtRepoConf.csv -NoTypeInformation   

Upvotes: 2

Jawad
Jawad

Reputation: 11364

I have tried in the past with artifactory and -Credential doesnt really worked for me.

I tried the API way which is much simpler and easier to use.

Connecting to Artifactory with API Key

Read here to find out how you can get the API key for your account on artifactory.

$header = @{"X-JFrog-Art-Api" = "yourAPIKey"}
Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Headers $header

Using Basic Auth and -Credential

If you do want to work with the Get-Credential prompt, make sure you use the username that works in Artifactory. Its not the same as the domain\user. from here

$login = Get-Credential -Message "Enter Credentials for Artifactory"

#invalid creds.. but its ok. Need to tell invoke-restmethod to use Basic Auth.
$headers = @{ Authorization = "Basic Zm9vOmJhcg==" }  

# use -Credential to override the credentials.
$new = Invoke-RestMethod -URI https://<server>/artifactory/api/repositories -Headers $headers -Credential $login

Upvotes: 2

Related Questions