/
login_info.go
184 lines (159 loc) · 5.39 KB
/
login_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package azure
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure/auth"
log "github.com/sirupsen/logrus"
)
const (
msiTokenEndpoint = "http://169.254.169.254/metadata/identity/oauth2/token"
)
// LoginType is the type of login used
type LoginType int
const (
// ServicePrincipal - logged in using Service Principal
ServicePrincipal LoginType = iota
// DeviceCode - logged in using DeviceCode Flow
DeviceCode
// CloudShell - logged in using DeviceCode Flow
CloudShell
// MSI - logged in using DeviceCode Flow
MSI
// CLI - logged in using DeviceCode Flow
CLI
)
// LoginInfo contains Azure login information
type LoginInfo struct {
Authorizer autorest.Authorizer
LoginType LoginType
OAuthTokenProvider adal.OAuthTokenProvider
}
// LoginToAzure attempts to login to azure
func LoginToAzure(clientID string, clientSecret string, tenantID string, applicationID string) (LoginInfo, error) {
var loginInfo LoginInfo
var err error
// Attempt to login with Service Principal
if len(clientID) != 0 && len(clientSecret) != 0 && len(tenantID) != 0 {
log.Debug("Attempting to Login with Service Principal")
clientCredentailsConfig := auth.NewClientCredentialsConfig(clientID, clientSecret, tenantID)
loginInfo.Authorizer, err = clientCredentailsConfig.Authorizer()
if err != nil {
return loginInfo, fmt.Errorf("Attempt to set Authorizer with Service Principal failed: %v", err)
}
log.Debug("Logged in with Service Principal")
loginInfo.LoginType = ServicePrincipal
return loginInfo, nil
}
// Attempt to login with Device Code
if len(applicationID) != 0 && len(tenantID) != 0 {
log.Debug("Attempting to Login with Device Code")
deviceFlowConfig := auth.NewDeviceFlowConfig(applicationID, tenantID)
loginInfo.OAuthTokenProvider, err = deviceFlowConfig.ServicePrincipalToken()
if err != nil {
return loginInfo, fmt.Errorf("failed to get oauth token from device flow: %v", err)
}
log.Debug("Logged in with Device Code")
loginInfo.Authorizer = autorest.NewBearerAuthorizer(loginInfo.OAuthTokenProvider)
loginInfo.LoginType = DeviceCode
return loginInfo, nil
}
// Attempt to use token from CloudShell
if IsInCloudShell() {
log.Debug("Attempting to Login with CloudShell")
loginInfo.OAuthTokenProvider, err = GetCloudShellToken()
if err != nil {
return loginInfo, fmt.Errorf("Attempt to get CloudShell token failed: %v", err)
}
log.Debug("Logged in with CloudShell")
loginInfo.Authorizer = autorest.NewBearerAuthorizer(loginInfo.OAuthTokenProvider)
loginInfo.LoginType = CloudShell
return loginInfo, nil
}
// Attempt to login with MSI
if checkForMSIEndpoint() {
log.Debug("Attempting to Login with MSI")
msiConfig := auth.NewMSIConfig()
loginInfo.Authorizer, err = msiConfig.Authorizer()
if err != nil {
return loginInfo, fmt.Errorf("Attempt to set Authorizer with MSI failed: %v", err)
}
loginInfo.LoginType = MSI
log.Debug("Logged in with MSI")
return loginInfo, nil
}
// Attempt to Login using azure CLI
log.Debug("Attempting to Login with az cli")
loginInfo.Authorizer, err = auth.NewAuthorizerFromCLI()
if err == nil {
loginInfo.LoginType = CLI
log.Debug("Logged in with CLI")
return loginInfo, nil
}
return loginInfo, fmt.Errorf("Cannot login to Azure - no valid credentials provided or available, failed to login with Azure cli: %v", err)
}
//GetCloudShellToken gets the CloudShell Token
func GetCloudShellToken() (*adal.Token, error) {
MSIEndpoint := os.Getenv("MSI_ENDPOINT")
log.Debug("CloudShell MSI Endpoint: ", MSIEndpoint)
if len(MSIEndpoint) == 0 {
return nil, errors.New("MSI_ENDPOINT environment variable not set")
}
MSIAudience := os.Getenv("CNAB_AZURE_MSI_AUDIENCE")
if len(MSIAudience) == 0 {
MSIAudience = "https://management.azure.com/"
}
log.Debug("CloudShell MSI Audience: ", MSIAudience)
timeout := time.Duration(1 * time.Second)
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest("GET", MSIEndpoint, nil)
if err != nil {
return nil, fmt.Errorf("Error creating HTTP Request to CloudShell Token: %v", err)
}
req.Header.Set("Metadata", "true")
query := req.URL.Query()
query.Add("api-version", "2018-02-01")
query.Add("resource", MSIAudience)
req.URL.RawQuery = query.Encode()
log.Debug("Cloud Shell Token URI: ", req.RequestURI)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Error getting CloudShell Token: %v", err)
}
defer resp.Body.Close()
rawResp, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
if err != nil {
return nil, fmt.Errorf("Error getting CloudShell Token. Status Code:'%d'. Failed reading response body error: %v", resp.StatusCode, err)
}
return nil, fmt.Errorf("Error getting Token. Status Code:'%d'. Response body: %s", resp.StatusCode, string(rawResp))
}
var token adal.Token
err = json.Unmarshal(rawResp, &token)
if err != nil {
return nil, fmt.Errorf("Error deserialising CloudShell Token Status Code: '%d'. Token: %s", resp.StatusCode, string(rawResp))
}
return &token, nil
}
func checkForMSIEndpoint() bool {
var err error
for i := 1; i < 4; i++ {
timeout := time.Duration(time.Duration(i) * time.Second)
client := http.Client{
Timeout: timeout,
}
_, err = client.Head(msiTokenEndpoint)
if err != nil {
break
}
}
return err == nil
}