/
logging.go
88 lines (79 loc) · 2.89 KB
/
logging.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azidentity
import (
"fmt"
"os"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/internal/runtime"
)
// LogCredential entries contain information about authentication.
// This includes information like the names of environment variables
// used when obtaining credentials and the type of credential used.
const LogCredential azcore.LogClassification = "Credential"
// log environment variables that can be used for credential types
func logEnvVars() {
if !azcore.Log().Should(LogCredential) {
return
}
// Log available environment variables
envVars := []string{}
if envCheck := os.Getenv("AZURE_TENANT_ID"); len(envCheck) > 0 {
envVars = append(envVars, "AZURE_TENANT_ID")
}
if envCheck := os.Getenv("AZURE_CLIENT_ID"); len(envCheck) > 0 {
envVars = append(envVars, "AZURE_CLIENT_ID")
}
if envCheck := os.Getenv("AZURE_CLIENT_SECRET"); len(envCheck) > 0 {
envVars = append(envVars, "AZURE_CLIENT_SECRET")
}
if envCheck := os.Getenv("AZURE_AUTHORITY_HOST"); len(envCheck) > 0 {
envVars = append(envVars, "AZURE_AUTHORITY_HOST")
}
if envCheck := os.Getenv("AZURE_CLI_PATH"); len(envCheck) > 0 {
envVars = append(envVars, "AZURE_CLI_PATH")
}
if len(envVars) > 0 {
azcore.Log().Writef(LogCredential, "Azure Identity => Found the following environment variables:\n\t%s", strings.Join(envVars, ", "))
}
}
func logGetTokenSuccess(cred azcore.TokenCredential, opts azcore.TokenRequestOptions) {
if !azcore.Log().Should(LogCredential) {
return
}
msg := fmt.Sprintf("Azure Identity => GetToken() result for %T: SUCCESS\n", cred)
msg += fmt.Sprintf("\tCredential Scopes: [%s]", strings.Join(opts.Scopes, ", "))
azcore.Log().Write(LogCredential, msg)
}
func logCredentialError(credName string, err error) {
azcore.Log().Writef(LogCredential, "Azure Identity => ERROR in %s: %s", credName, err.Error())
}
func logMSIEnv(msi msiType) {
if !azcore.Log().Should(LogCredential) {
return
}
var msg string
switch msi {
case msiTypeIMDS:
msg = "Azure Identity => Managed Identity environment: IMDS"
case msiTypeAppServiceV20170901, msiTypeCloudShell, msiTypeAppServiceV20190801:
msg = "Azure Identity => Managed Identity environment: MSI_ENDPOINT"
case msiTypeUnavailable:
msg = "Azure Identity => Managed Identity environment: Unavailable"
default:
msg = "Azure Identity => Managed Identity environment: Unknown"
}
azcore.Log().Write(LogCredential, msg)
}
func addGetTokenFailureLogs(credName string, err error, includeStack bool) {
if !azcore.Log().Should(LogCredential) {
return
}
stack := ""
if includeStack {
// skip the stack trace frames and ourself
stack = "\n" + runtime.StackTrace(3, azcore.StackFrameCount)
}
azcore.Log().Writef(LogCredential, "Azure Identity => ERROR in GetToken() call for %s: %s%s", credName, err.Error(), stack)
}