azure: acr docker cred provider reuses auth

This commit is contained in:
Cole Mickens 2017-07-13 03:15:27 -07:00
parent 8f55afd0cb
commit 931002ec1f
3 changed files with 21 additions and 36 deletions

View File

@ -17,11 +17,9 @@ go_library(
"//pkg/credentialprovider:go_default_library",
"//vendor/github.com/Azure/azure-sdk-for-go/arm/containerregistry:go_default_library",
"//vendor/github.com/Azure/go-autorest/autorest:go_default_library",
"//vendor/github.com/Azure/go-autorest/autorest/adal:go_default_library",
"//vendor/github.com/Azure/go-autorest/autorest/azure:go_default_library",
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/spf13/pflag:go_default_library",
"//vendor/gopkg.in/yaml.v2:go_default_library",
],
)

View File

@ -17,14 +17,12 @@ limitations under the License.
package azure
import (
"io/ioutil"
"io"
"os"
"time"
yaml "gopkg.in/yaml.v2"
"github.com/Azure/azure-sdk-for-go/arm/containerregistry"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
azureapi "github.com/Azure/go-autorest/autorest/azure"
"github.com/golang/glog"
"github.com/spf13/pflag"
@ -47,10 +45,12 @@ func init() {
})
}
// RegistriesClient is a testable interface for the ACR client List operation.
type RegistriesClient interface {
List() (containerregistry.RegistryListResult, error)
}
// NewACRProvider parses the specified configFile and returns a DockerConfigProvider
func NewACRProvider(configFile *string) credentialprovider.DockerConfigProvider {
return &acrProvider{
file: configFile,
@ -59,24 +59,16 @@ func NewACRProvider(configFile *string) credentialprovider.DockerConfigProvider
type acrProvider struct {
file *string
config azure.Config
environment azureapi.Environment
config *azure.Config
environment *azureapi.Environment
registryClient RegistriesClient
}
func (a *acrProvider) loadConfig(contents []byte) error {
err := yaml.Unmarshal(contents, &a.config)
func (a *acrProvider) loadConfig(rdr io.Reader) error {
var err error
a.config, a.environment, err = azure.ParseConfig(rdr)
if err != nil {
return err
}
if a.config.Cloud == "" {
a.environment = azureapi.PublicCloud
} else {
a.environment, err = azureapi.EnvironmentFromName(a.config.Cloud)
if err != nil {
return err
}
glog.Errorf("Failed to load azure credential file: %v", err)
}
return nil
}
@ -86,27 +78,21 @@ func (a *acrProvider) Enabled() bool {
glog.V(5).Infof("Azure config unspecified, disabling")
return false
}
contents, err := ioutil.ReadFile(*a.file)
f, err := os.Open(*a.file)
if err != nil {
glog.Errorf("Failed to load azure credential file: %v", err)
glog.Errorf("Failed to load config from file: %s", *a.file)
return false
}
if err := a.loadConfig(contents); err != nil {
glog.Errorf("Failed to parse azure credential file: %v", err)
defer f.Close()
err = a.loadConfig(f)
if err != nil {
glog.Errorf("Failed to load config from file: %s", *a.file)
return false
}
oauthConfig, err := adal.NewOAuthConfig(a.environment.ActiveDirectoryEndpoint, a.config.TenantID)
if err != nil {
glog.Errorf("Failed to get oauth config: %v", err)
return false
}
servicePrincipalToken, err := adal.NewServicePrincipalToken(
*oauthConfig,
a.config.AADClientID,
a.config.AADClientSecret,
a.environment.ServiceManagementEndpoint)
servicePrincipalToken, err := azure.GetServicePrincipalToken(a.config, a.environment)
if err != nil {
glog.Errorf("Failed to create service principal token: %v", err)
return false

View File

@ -17,6 +17,7 @@ limitations under the License.
package azure
import (
"bytes"
"testing"
"github.com/Azure/azure-sdk-for-go/arm/containerregistry"
@ -66,7 +67,7 @@ func Test(t *testing.T) {
provider := &acrProvider{
registryClient: fakeClient,
}
provider.loadConfig([]byte(configStr))
provider.loadConfig(bytes.NewBufferString(configStr))
creds := provider.Provide()