|
1 | 1 | package client
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 | 4 | "fmt"
|
6 | 5 | "io"
|
7 | 6 | "net/http"
|
8 | 7 | "strings"
|
9 | 8 |
|
10 |
| - "github.com/hashicorp/go-multierror" |
11 | 9 | "github.com/jetstack/preflight/api"
|
12 | 10 | )
|
13 | 11 |
|
14 |
| -var ( |
15 |
| - // ClientID is the auth0 client identifier (injected at build time) |
16 |
| - ClientID string |
17 |
| - |
18 |
| - // ClientSecret is the auth0 client secret (injected at build time) |
19 |
| - ClientSecret string |
20 |
| - |
21 |
| - // AuthServerDomain is the auth0 domain (injected at build time) |
22 |
| - AuthServerDomain string |
23 |
| -) |
24 |
| - |
25 | 12 | type (
|
26 | 13 | // The Client interface describes types that perform requests against the Jetstack Secure backend.
|
27 | 14 | Client interface {
|
28 | 15 | PostDataReadings(orgID, clusterID string, readings []*api.DataReading) error
|
29 | 16 | Post(path string, body io.Reader) (*http.Response, error)
|
30 | 17 | }
|
31 |
| - |
32 |
| - // Credentials defines the format of the credentials.json file. |
33 |
| - Credentials struct { |
34 |
| - // UserID is the ID or email for the user or service account. |
35 |
| - UserID string `json:"user_id"` |
36 |
| - // UserSecret is the secret for the user or service account. |
37 |
| - UserSecret string `json:"user_secret"` |
38 |
| - // The following fields are optional as the default behaviour |
39 |
| - // is to use the equivalent variables defined at package level |
40 |
| - // and injected at build time. |
41 |
| - // ClientID is the oauth2 client ID. |
42 |
| - ClientID string `json:"client_id,omitempty"` |
43 |
| - // ClientSecret is the oauth2 client secret. |
44 |
| - ClientSecret string `json:"client_secret,omitempty"` |
45 |
| - // AuthServerDomain is the domain for the auth server. |
46 |
| - AuthServerDomain string `json:"auth_server_domain,omitempty"` |
47 |
| - } |
48 | 18 | )
|
49 | 19 |
|
50 |
| -// ParseCredentials reads credentials into a struct used. Performs validations. |
51 |
| -func ParseCredentials(data []byte) (*Credentials, error) { |
52 |
| - var credentials Credentials |
53 |
| - |
54 |
| - err := json.Unmarshal(data, &credentials) |
55 |
| - if err != nil { |
56 |
| - return nil, err |
57 |
| - } |
58 |
| - |
59 |
| - if err = credentials.validate(); err != nil { |
60 |
| - return nil, err |
61 |
| - } |
62 |
| - |
63 |
| - return &credentials, nil |
64 |
| -} |
65 |
| - |
66 |
| -// IsClientSet returns whether the client credentials are set or not. |
67 |
| -func (c *Credentials) IsClientSet() bool { |
68 |
| - return c.ClientID != "" && c.ClientSecret != "" && c.AuthServerDomain != "" |
69 |
| -} |
70 |
| - |
71 |
| -func (c *Credentials) validate() error { |
72 |
| - var result *multierror.Error |
73 |
| - |
74 |
| - if c == nil { |
75 |
| - return fmt.Errorf("credentials are nil") |
76 |
| - } |
77 |
| - |
78 |
| - if c.UserID == "" { |
79 |
| - result = multierror.Append(result, fmt.Errorf("user_id cannot be empty")) |
80 |
| - } |
81 |
| - |
82 |
| - if c.UserSecret == "" { |
83 |
| - result = multierror.Append(result, fmt.Errorf("user_secret cannot be empty")) |
84 |
| - } |
85 |
| - |
86 |
| - return result.ErrorOrNil() |
87 |
| -} |
88 |
| - |
89 | 20 | func fullURL(baseURL, path string) string {
|
90 | 21 | base := baseURL
|
91 | 22 | for strings.HasSuffix(base, "/") {
|
|
0 commit comments