| 1 | /* |
| 2 | Copyright © 2020 Chris Mellard [email protected] |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | package credhelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ |
| 17 | |
| 18 | import ( |
| 19 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 20 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 21 | "net/urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/" |
| 22 | "regexpscip-go gomod github.com/golang/go/src go1.18 regexp/" |
| 23 | |
| 24 | "github.com/Azure/go-autorest/autorest/azure/authscip-go gomod github.com/Azure/go-autorest/autorest/azure/auth v0.5.2 `github.com/Azure/go-autorest/autorest/azure/auth`/" |
| 25 | "github.com/chrismellard/docker-credential-acr-env/pkg/registryscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/registry`/" |
| 26 | "github.com/chrismellard/docker-credential-acr-env/pkg/tokenscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/token`/" |
| 27 | "github.com/docker/docker-credential-helpers/credentialsscip-go gomod github.com/docker/docker-credential-helpers v0.6.3 `github.com/docker/docker-credential-helpers/credentials`/" |
| 28 | ) |
| 29 | |
| 30 | var acrREscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/acrRE. = regexpscip-go gomod github.com/golang/go/src go1.18 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.18 regexp/MustCompile().(`.*\.azurecr\.io|.*\.azurecr\.cn|.*\.azurecr\.de|.*\.azurecr\.us`) |
| 31 | |
| 32 | const ( |
| 33 | mcrHostnamescip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/mcrHostname. = "mcr.microsoft.com" |
| 34 | tokenUsernamescip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/tokenUsername. = "<token>" |
| 35 | ) |
| 36 | |
| 37 | type ACRCredHelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper# struct { |
| 38 | } |
| 39 | |
| 40 | func NewACRCredentialsHelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/NewACRCredentialsHelper().() credentialsscip-go gomod github.com/docker/docker-credential-helpers v0.6.3 `github.com/docker/docker-credential-helpers/credentials`/.Helperscip-go gomod github.com/docker/docker-credential-helpers v0.6.3 `github.com/docker/docker-credential-helpers/credentials`/Helper# { |
| 41 | return &ACRCredHelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#{} |
| 42 | } |
| 43 | |
| 44 | func (a ACRCredHelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#) Addscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#Add().(_ *credentialsscip-go gomod github.com/docker/docker-credential-helpers v0.6.3 `github.com/docker/docker-credential-helpers/credentials`/.Credentialsscip-go gomod github.com/docker/docker-credential-helpers v0.6.3 `github.com/docker/docker-credential-helpers/credentials`/Credentials#) error { |
| 45 | return errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("list is unimplemented") |
| 46 | } |
| 47 | |
| 48 | func (a ACRCredHelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#) Deletescip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#Delete().(_ string) error { |
| 49 | return errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("list is unimplemented") |
| 50 | } |
| 51 | |
| 52 | func isACRRegistryscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/isACRRegistry().(input string) bool { |
| 53 | serverURL, err := urlscip-go gomod github.com/golang/go/src go1.18 `net/url`/.Parsescip-go gomod github.com/golang/go/src go1.18 `net/url`/Parse().("https://" + input) |
| 54 | if err != nil { |
| 55 | return false |
| 56 | } |
| 57 | if serverURL.Hostnamescip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Hostname().() == mcrHostnamescip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/mcrHostname. { |
| 58 | return true |
| 59 | } |
| 60 | matches := acrREscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/acrRE..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.18 regexp/Regexp#FindStringSubmatch().(serverURL.Hostnamescip-go gomod github.com/golang/go/src go1.18 `net/url`/URL#Hostname().()) |
| 61 | if len(matches) == 0 { |
| 62 | return false |
| 63 | } |
| 64 | return true |
| 65 | } |
| 66 | |
| 67 | func (a ACRCredHelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#) Getscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#Get().(serverURL string) (string, string, error) { |
| 68 | if !isACRRegistryscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/isACRRegistry().(serverURL) { |
| 69 | return "", "", errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("serverURL does not refer to Azure Container Registry") |
| 70 | } |
| 71 | |
| 72 | spToken, settings, err := tokenscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/token`/.GetServicePrincipalTokenFromEnvironmentscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/token`/GetServicePrincipalTokenFromEnvironment().() |
| 73 | if err != nil { |
| 74 | return "", "", fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("failed to acquire sp token %w", err) |
| 75 | } |
| 76 | refreshToken, err := registryscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/registry`/.GetRegistryRefreshTokenFromAADExchangescip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/registry`/GetRegistryRefreshTokenFromAADExchange().(serverURL, spToken, settings.Valuesscip-go gomod github.com/Azure/go-autorest/autorest/azure/auth v0.5.2 `github.com/Azure/go-autorest/autorest/azure/auth`/EnvironmentSettings#Values.[authscip-go gomod github.com/Azure/go-autorest/autorest/azure/auth v0.5.2 `github.com/Azure/go-autorest/autorest/azure/auth`/.TenantIDscip-go gomod github.com/Azure/go-autorest/autorest/azure/auth v0.5.2 `github.com/Azure/go-autorest/autorest/azure/auth`/TenantID.]) |
| 77 | if err != nil { |
| 78 | return "", "", fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("failed to acquire refresh token %w", err) |
| 79 | } |
| 80 | return tokenUsernamescip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/tokenUsername., refreshToken, nil |
| 81 | } |
| 82 | |
| 83 | func (a ACRCredHelperscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#) Listscip-go gomod github.com/chrismellard/docker-credential-acr-env . `github.com/chrismellard/docker-credential-acr-env/pkg/credhelper`/ACRCredHelper#List().() (map[string]string, error) { |
| 84 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 errors/New().("list is unimplemented") |
| 85 | } |