/
token.go
53 lines (48 loc) · 1.58 KB
/
token.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
package testutil
import (
"bytes"
"fmt"
"sync"
"github.com/pachyderm/pachyderm/src/server/pkg/backoff"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var (
cachedEnterpriseCodeOnce sync.Once
cachedEnterpriseCode string
)
// GetTestEnterpriseCode gets a Pachyderm Enterprise activation code from a
// private S3 bucket, and provides it to tests that use Pachyderm Enterprise
// features
func GetTestEnterpriseCode() string {
cachedEnterpriseCodeOnce.Do(func() {
// Get test enterprise code from s3. The Pachyderm Enterprise test activation
// token is stored in
// s3://pachyderm-engineering/test_enterprise_activation_code.txt
s3client := s3.New(session.Must(session.NewSessionWithOptions(
session.Options{
Config: aws.Config{
Region: aws.String("us-west-1"), // contains s3://pachyderm-engineering
},
})))
var s3Resp *s3.GetObjectOutput
if err := backoff.Retry(func() error {
var err error
s3Resp, err = s3client.GetObject(&s3.GetObjectInput{
Bucket: aws.String("pachyderm-engineering"),
Key: aws.String("test_enterprise_activation_code.txt"),
})
return err
}, backoff.NewTestingBackOff()); err != nil {
// tests can't run without credentials -- just crash
panic(fmt.Sprintf("cannot get test enterprise token from s3: %v", err))
}
buf := &bytes.Buffer{}
if _, err := buf.ReadFrom(s3Resp.Body); err != nil {
panic(fmt.Sprintf("cannot copy test enterprise token to buffer: %v", err))
}
cachedEnterpriseCode = buf.String()
})
return cachedEnterpriseCode
}