forked from hashicorp/vault-plugin-auth-azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure_test.go
91 lines (75 loc) · 2.29 KB
/
azure_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
79
80
81
82
83
84
85
86
87
88
89
90
91
package azureauth
import (
"context"
"encoding/base64"
"errors"
"fmt"
"strings"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2019-07-01/compute"
oidc "github.com/coreos/go-oidc"
)
// mockKeySet is used in tests to bypass signature validation and return only
// the jwt payload
type mockKeySet struct{}
func (s *mockKeySet) VerifySignature(ctx context.Context, idToken string) ([]byte, error) {
parts := strings.Split(idToken, ".")
if len(parts) != 3 {
return nil, errors.New("invalid jwt")
}
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return nil, fmt.Errorf("error decoding payload: %v", err)
}
return payload, nil
}
func newMockVerifier() tokenVerifier {
config := &oidc.Config{
SkipClientIDCheck: true,
SkipExpiryCheck: false,
}
ks := new(mockKeySet)
return oidc.NewVerifier("", ks, config)
}
type mockComputeClient struct {
computeClientFunc func(vmName string) (compute.VirtualMachine, error)
}
type mockVMSSClient struct {
vmssClientFunc func(vmssName string) (compute.VirtualMachineScaleSet, error)
}
func (c *mockComputeClient) Get(ctx context.Context, resourceGroup, vmName string, instanceView compute.InstanceViewTypes) (compute.VirtualMachine, error) {
if c.computeClientFunc != nil {
return c.computeClientFunc(vmName)
}
return compute.VirtualMachine{}, nil
}
func (c *mockVMSSClient) Get(ctx context.Context, resourceGroup, vmssName string) (compute.VirtualMachineScaleSet, error) {
if c.vmssClientFunc != nil {
return c.vmssClientFunc(vmssName)
}
return compute.VirtualMachineScaleSet{}, nil
}
type computeClientFunc func(vmName string) (compute.VirtualMachine, error)
type vmssClientFunc func(vmssName string) (compute.VirtualMachineScaleSet, error)
type mockProvider struct {
computeClientFunc
vmssClientFunc
}
func newMockProvider(c computeClientFunc, v vmssClientFunc) *mockProvider {
return &mockProvider{
computeClientFunc: c,
vmssClientFunc: v,
}
}
func (*mockProvider) Verifier() tokenVerifier {
return newMockVerifier()
}
func (p *mockProvider) ComputeClient(string) (computeClient, error) {
return &mockComputeClient{
computeClientFunc: p.computeClientFunc,
}, nil
}
func (p *mockProvider) VMSSClient(string) (vmssClient, error) {
return &mockVMSSClient{
vmssClientFunc: p.vmssClientFunc,
}, nil
}