2018-11-21 22:06:03 +01:00
|
|
|
package authentication
|
|
|
|
|
|
|
|
import (
|
2019-11-26 01:03:57 +01:00
|
|
|
"strings"
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
"github.com/Azure/go-autorest/autorest/azure/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
type azureCLIProfile struct {
|
|
|
|
profile cli.Profile
|
|
|
|
|
2019-11-26 01:03:57 +01:00
|
|
|
clientId string
|
|
|
|
environment string
|
|
|
|
subscriptionId string
|
|
|
|
tenantId string
|
2018-11-21 22:06:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *azureCLIProfile) populateFields() error {
|
|
|
|
// ensure we know the Subscription ID - since it's needed for everything else
|
|
|
|
if a.subscriptionId == "" {
|
|
|
|
err := a.populateSubscriptionID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.tenantId == "" {
|
|
|
|
// now we know the subscription ID, find the associated Tenant ID
|
|
|
|
err := a.populateTenantID()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// always pull the environment from the Azure CLI, since the Access Token's associated with it
|
|
|
|
return a.populateEnvironment()
|
|
|
|
}
|
2019-11-26 01:03:57 +01:00
|
|
|
|
|
|
|
func (a *azureCLIProfile) verifyAuthenticatedAsAUser() bool {
|
|
|
|
for _, subscription := range a.profile.Subscriptions {
|
|
|
|
if subscription.User == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
authenticatedAsAUser := strings.EqualFold(subscription.User.Type, "user")
|
|
|
|
if authenticatedAsAUser {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|