-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
78 lines (63 loc) · 1.59 KB
/
test.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
package main
import (
"fmt"
"github.com/Azure/azure-sdk-for-go/dataplane/keyvault"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/Azure/go-autorest/autorest/azure"
)
var (
vaultURL = fmt.Sprintf("https://%s.vault.azure.net", "<vaultName>")
keyName = "<keyName>"
clientID = "<clientID>"
)
func AuthFromDeviceFlow(clientID, resource string) (autorest.Authorizer, error) {
oauthClient := &autorest.Client{}
oauthConfig, err := adal.NewOAuthConfig(azure.PublicCloud.ActiveDirectoryEndpoint, "common")
if err != nil {
return nil, err
}
deviceCode, err := adal.InitiateDeviceAuth(oauthClient, *oauthConfig, clientID, resource)
if err != nil {
return nil, err
}
fmt.Println(*deviceCode.Message)
token, err := adal.WaitForUserCompletion(oauthClient, deviceCode)
if err != nil {
return nil, err
}
spt, err := adal.NewServicePrincipalTokenFromManualToken(
*oauthConfig,
clientID,
resource,
*token,
)
if err != nil {
return nil, err
}
return autorest.NewBearerAuthorizer(spt), nil
}
func KVTest(resource string) error {
authorizer, err := AuthFromDeviceFlow(clientID, resource)
if err != nil {
return err
}
kv := keyvault.New()
kv.Authorizer = authorizer
str := "abc"
_, err = kv.Encrypt(vaultURL, keyName, "", keyvault.KeyOperationsParameters{
Algorithm: keyvault.RSAOAEP,
Value: &str,
})
return err
}
func main() {
//no error
if err := KVTest("https://vault.azure.net"); err != nil {
fmt.Println(err)
}
//error
if err := KVTest(azure.PublicCloud.KeyVaultEndpoint); err != nil {
fmt.Println(err)
}
}