-
Notifications
You must be signed in to change notification settings - Fork 46
/
acctest.go
75 lines (66 loc) · 2.25 KB
/
acctest.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package test
import (
"context"
"net/http"
"os"
"testing"
"github.com/hashicorp/go-azure-sdk/sdk/auth"
"github.com/hashicorp/go-azure-sdk/sdk/claims"
"github.com/hashicorp/go-azure-sdk/sdk/environments"
)
func AccTest(t *testing.T) {
if testing.Short() {
t.Skip("skipping acceptance test when `-short` is set")
}
if os.Getenv("ACCTEST") == "" {
t.Skip("skipping acceptance test, ACCTEST is not set")
}
}
type Connection struct {
AuthConfig auth.Credentials
Authorizer auth.Authorizer
Claims *claims.Claims
}
// NewConnection configures and returns a Connection for use in tests.
func NewConnection(t *testing.T) *Connection {
env, err := environments.FromName(Environment)
if err != nil {
t.Fatal(err)
}
return &Connection{
AuthConfig: auth.Credentials{
Environment: *env,
TenantID: TenantId,
ClientID: ClientId,
ClientCertificateData: Base64DecodeCertificate(t, ClientCertificate),
ClientCertificatePath: ClientCertificatePath,
ClientCertificatePassword: ClientCertPassword,
ClientSecret: ClientSecret,
GitHubOIDCTokenRequestURL: GitHubTokenURL,
GitHubOIDCTokenRequestToken: GitHubToken,
OIDCAssertionToken: IdToken,
CustomManagedIdentityEndpoint: CustomManagedIdentityEndpoint,
EnableAuthenticatingUsingClientCertificate: true,
EnableAuthenticatingUsingClientSecret: true,
EnableAuthenticationUsingGitHubOIDC: true,
},
}
}
// Authorize configures an Authorizer for the Connection
func (c *Connection) Authorize(ctx context.Context, t *testing.T, api environments.Api) {
var err error
c.Authorizer, err = auth.NewAuthorizerFromCredentials(ctx, c.AuthConfig, api)
if err != nil {
t.Fatalf("building authorizer from credentials: %+v", err)
}
token, err := c.Authorizer.Token(ctx, &http.Request{})
if err != nil {
t.Fatalf("acquiring access token: %v", err)
}
c.Claims, err = claims.ParseClaims(token)
if err != nil {
t.Fatalf("parsing token claims: %v", err)
}
}