File: plugin.go

package info (click to toggle)
golang-github-aws-aws-sdk-go 1.16.18%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, buster-backports, experimental
  • size: 93,084 kB
  • sloc: ruby: 193; makefile: 174; xml: 11
file content (61 lines) | stat: -rw-r--r-- 1,511 bytes parent folder | download | duplicates (2)
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
// +build example,go18

package main

import (
	"encoding/json"
	"os"

	"github.com/pkg/errors"
)

// Example plugin that will retrieve credentials from a JSON file that the
// "PLUGIN_CREDS_FILE" environment variable points to
//
// Build with:
//   go build -tags example -o plugin.so -buildmode=plugin plugin.go
func main() {}

var myCredProvider provider

func init() {
	// Initialize a mock credential provider with stubs
	myCredProvider = provider{Filename: os.Getenv("PLUGIN_CREDS_FILE")}
}

// GetAWSSDKCredentialProvider is the symbol SDK will lookup and use to
// get the credential provider's retrieve and isExpired functions.
func GetAWSSDKCredentialProvider() (func() (key, secret, token string, err error), func() bool) {
	return myCredProvider.Retrieve, myCredProvider.IsExpired
}

// mock implementation of a type that returns retrieves credentials and
// returns if they have expired.
type provider struct {
	Filename string

	loaded bool
}

func (p *provider) Retrieve() (key, secret, token string, err error) {
	f, err := os.Open(p.Filename)
	if err != nil {
		return "", "", "", errors.Wrapf(err, "failed to open credentials file, %q", p.Filename)
	}
	decoder := json.NewDecoder(f)

	creds := struct {
		Key, Secret, Token string
	}{}

	if err := decoder.Decode(&creds); err != nil {
		return "", "", "", errors.Wrap(err, "failed to decode credentials file")
	}

	p.loaded = true
	return creds.Key, creds.Secret, creds.Token, nil
}

func (p *provider) IsExpired() bool {
	return !p.loaded
}