...

Source file src/github.com/chrismellard/docker-credential-acr-env/pkg/credhelper/helper.go

Documentation: github.com/chrismellard/docker-credential-acr-env/pkg/credhelper

     1  /*
     2  Copyright © 2020 Chris Mellard chris.mellard@icloud.com
     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 credhelper
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"net/url"
    22  	"regexp"
    23  
    24  	"github.com/Azure/go-autorest/autorest/azure/auth"
    25  	"github.com/chrismellard/docker-credential-acr-env/pkg/registry"
    26  	"github.com/chrismellard/docker-credential-acr-env/pkg/token"
    27  	"github.com/docker/docker-credential-helpers/credentials"
    28  )
    29  
    30  var acrRE = regexp.MustCompile(`.*\.azurecr\.io|.*\.azurecr\.cn|.*\.azurecr\.de|.*\.azurecr\.us`)
    31  
    32  const (
    33  	mcrHostname   = "mcr.microsoft.com"
    34  	tokenUsername = "<token>"
    35  )
    36  
    37  type ACRCredHelper struct {
    38  }
    39  
    40  func NewACRCredentialsHelper() credentials.Helper {
    41  	return &ACRCredHelper{}
    42  }
    43  
    44  func (a ACRCredHelper) Add(_ *credentials.Credentials) error {
    45  	return errors.New("list is unimplemented")
    46  }
    47  
    48  func (a ACRCredHelper) Delete(_ string) error {
    49  	return errors.New("list is unimplemented")
    50  }
    51  
    52  func isACRRegistry(input string) bool {
    53  	serverURL, err := url.Parse("https://" + input)
    54  	if err != nil {
    55  		return false
    56  	}
    57  	if serverURL.Hostname() == mcrHostname {
    58  		return true
    59  	}
    60  	matches := acrRE.FindStringSubmatch(serverURL.Hostname())
    61  	if len(matches) == 0 {
    62  		return false
    63  	}
    64  	return true
    65  }
    66  
    67  func (a ACRCredHelper) Get(serverURL string) (string, string, error) {
    68  	if !isACRRegistry(serverURL) {
    69  		return "", "", errors.New("serverURL does not refer to Azure Container Registry")
    70  	}
    71  
    72  	spToken, settings, err := token.GetServicePrincipalTokenFromEnvironment()
    73  	if err != nil {
    74  		return "", "", fmt.Errorf("failed to acquire sp token %w", err)
    75  	}
    76  	refreshToken, err := registry.GetRegistryRefreshTokenFromAADExchange(serverURL, spToken, settings.Values[auth.TenantID])
    77  	if err != nil {
    78  		return "", "", fmt.Errorf("failed to acquire refresh token %w", err)
    79  	}
    80  	return tokenUsername, refreshToken, nil
    81  }
    82  
    83  func (a ACRCredHelper) List() (map[string]string, error) {
    84  	return nil, errors.New("list is unimplemented")
    85  }
    86  

View as plain text