constellation/internal/azureshared/appcredentials.go

52 lines
1.6 KiB
Go
Raw Normal View History

2022-06-07 10:27:55 -04:00
package azureshared
import (
"fmt"
"net/url"
)
// ApplicationCredentials is a set of Azure AD application credentials.
// It is the equivalent of a service account key in other cloud providers.
type ApplicationCredentials struct {
2022-08-29 08:18:05 -04:00
TenantID string
AppClientID string
ClientSecretValue string
Location string
2022-06-07 10:27:55 -04:00
}
// ApplicationCredentialsFromURI converts a cloudServiceAccountURI into Azure ApplicationCredentials.
func ApplicationCredentialsFromURI(cloudServiceAccountURI string) (ApplicationCredentials, error) {
uri, err := url.Parse(cloudServiceAccountURI)
if err != nil {
return ApplicationCredentials{}, err
}
if uri.Scheme != "serviceaccount" {
return ApplicationCredentials{}, fmt.Errorf("invalid service account URI: invalid scheme: %s", uri.Scheme)
}
if uri.Host != "azure" {
return ApplicationCredentials{}, fmt.Errorf("invalid service account URI: invalid host: %s", uri.Host)
}
query := uri.Query()
return ApplicationCredentials{
2022-08-29 08:18:05 -04:00
TenantID: query.Get("tenant_id"),
AppClientID: query.Get("client_id"),
ClientSecretValue: query.Get("client_secret"),
Location: query.Get("location"),
2022-06-07 10:27:55 -04:00
}, nil
}
// ToCloudServiceAccountURI converts the ApplicationCredentials into a cloud service account URI.
func (c ApplicationCredentials) ToCloudServiceAccountURI() string {
query := url.Values{}
query.Add("tenant_id", c.TenantID)
2022-08-29 08:18:05 -04:00
query.Add("client_id", c.AppClientID)
query.Add("client_secret", c.ClientSecretValue)
2022-06-07 10:27:55 -04:00
query.Add("location", c.Location)
uri := url.URL{
Scheme: "serviceaccount",
Host: "azure",
RawQuery: query.Encode(),
}
return uri.String()
}