-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
94 lines (74 loc) · 2.15 KB
/
config.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
92
93
94
package gcpkms
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
// Key consists of required information to retrieve the CGP KMS Key path.
type Key struct {
// Keyring is the name of your KMS keyring.
Keyring string `json:"Keyring"`
// Name is the name of the key in the Keyring.
Name string `json:"Name"`
// Version is the of the current key.
Version string `json:"Version"`
}
func (k Key) isValid() bool {
return k.Keyring != "" && k.Name != "" && k.Version != ""
}
// Config represents required information to create a Google Cloud KMS client.
type Config struct {
// ProjectID is the ID of the working GCP project.
ProjectID string `json:"ProjectID"`
// LocationID is the region ID of the project.
//
// Example: us-west1.
LocationID string `json:"LocationID"`
// CredentialLocation is the absolute path of the credential file downloaded from the GCP.
//
// Example: "/Users/SomeUser/.cred/gcp-credential.json".
// Leave this field empty if the environment varialbe `GOOGLE_APPLICATION_CREDENTIALS` has been set.
CredentialLocation string `json:"CredentialLocation,omitempty"`
// Key is the detail of the GCP KMS key.
Key Key `json:"Key"`
// ChainID is the ID of the target EVM chain.
//
// See https://chainlist.org.
ChainID uint64 `json:"ChainID"`
}
// IsValid checks if a Config is valid.
func (cfg Config) IsValid() (bool, error) {
if cfg.ProjectID == "" {
return false, fmt.Errorf("empty ProjectID")
}
if cfg.LocationID == "" {
return false, fmt.Errorf("empty LocationID")
}
if cfg.CredentialLocation == "" {
return false, fmt.Errorf("empty CredentialLocation")
}
if !cfg.Key.isValid() {
return false, fmt.Errorf("invalid Key")
}
return true, nil
}
// LoadConfigFromFile loads the config from the given config file.
func LoadConfigFromFile(filePath string) (*Config, error) {
f, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
var cfg Config
err = json.Unmarshal(f, &cfg)
if err != nil {
return nil, err
}
if cfg.CredentialLocation == "" {
cfg.CredentialLocation = os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
}
if _, err = cfg.IsValid(); err != nil {
return nil, err
}
return &cfg, nil
}