Add a banner to the package promotion logging.
[arvados-dev.git] / compute-image-cleaner / config / azure-config.go
1 // Copyright (C) The Azure-Samples Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: MIT
4
5 // Largely borrowed from
6 // https://github.com/Azure-Samples/azure-sdk-for-go-samples/tree/master/internal/config
7
8 package config
9
10 import (
11   "fmt"
12   "os"
13
14   "github.com/Azure/go-autorest/autorest/azure"
15 )
16
17 var (
18   clientID               string
19   clientSecret           string
20   tenantID               string
21   subscriptionID         string
22   cloudName              string = "AzurePublicCloud"
23   useDeviceFlow          bool
24   environment            *azure.Environment
25 )
26
27 // ClientID is the OAuth client ID.
28 func ClientID() string {
29   return clientID
30 }
31
32 // ClientSecret is the OAuth client secret.
33 func ClientSecret() string {
34   return clientSecret
35 }
36
37 // TenantID is the AAD tenant to which this client belongs.
38 func TenantID() string {
39   return tenantID
40 }
41
42 // SubscriptionID is a target subscription for Azure resources.
43 func SubscriptionID() string {
44   return subscriptionID
45 }
46
47 // UseDeviceFlow specifies if interactive auth should be used. Interactive
48 // auth uses the OAuth Device Flow grant type.
49 func UseDeviceFlow() bool {
50   return useDeviceFlow
51 }
52
53 // Environment returns an `azure.Environment{...}` for the current cloud.
54 func Environment() *azure.Environment {
55   if environment != nil {
56     return environment
57   }
58   env, err := azure.EnvironmentFromName(cloudName)
59   if err != nil {
60     // TODO: move to initialization of var
61     panic(fmt.Sprintf(
62       "invalid cloud name '%s' specified, cannot continue\n", cloudName))
63   }
64   environment = &env
65   return environment
66 }
67
68 // ParseEnvironment loads the Azure environment variables for authentication
69 func ParseEnvironment() error {
70   // these must be provided by environment
71   // clientID
72   clientID = os.Getenv("AZURE_CLIENT_ID")
73
74   // clientSecret
75   clientSecret = os.Getenv("AZURE_CLIENT_SECRET")
76
77   // tenantID (AAD)
78   tenantID = os.Getenv("AZURE_TENANT_ID")
79
80   // subscriptionID (ARM)
81   subscriptionID = os.Getenv("AZURE_SUBSCRIPTION_ID")
82
83   return nil
84 }