Debian Codegraph @

config/config.go

raw symbols SCIP
1 // Package config implements configuration structures for Red
2 // October.
3 package config
4
5 import (
6 "encoding/json"
7 "io/ioutil"
8 )
9
10 // Server contains the configuration information required to start a
11 // redoctober server.
12 type Server struct {
13 // Addr contains the host:port that the server should listen
14 // on.
15 Addr string `json:"address"`
16
17 // CAPath contains the path to the TLS CA for client
18 // authentication. This is an optional field.
19 CAPath string `json:"ca_path,omitempty"`
20
21 // KeyPaths and CertPaths contains a list of paths to TLS key
22 // pairs that should be used to secure connections to the
23 // server. The paths should be comma-separated.
24 KeyPaths string `json:"private_keys"`
25 CertPaths string `json:"certificates"`
26
27 // Systemd indicates whether systemd socket activation should
28 // be used instead of a normal port listener.
29 Systemd bool `json:"use_systemd,omitempty"`
30 }
31
32 // UI contains the configuration information for the WWW API.
33 type UI struct {
34 // Root contains the base URL for the UI.
35 Root string `json:"root"`
36
37 // Static is an optional path for overriding the built in HTML
38 // UI.
39 Static string `json:"static"`
40 }
41
42 // HipChat contains the settings for Hipchat integration.
43 type HipChat struct {
44 Host string `json:"host"`
45 Room string `json:"room"`
46 APIKey string `json:"api_key"`
47 }
48
49 // Valid returns true if the HipChat config is ready to be used for
50 // HipChat notifications.
51 func (hc *HipChat) Valid() bool {
52 if hc.APIKey == "" {
53 return false
54 }
55
56 if hc.Room == "" {
57 return false
58 }
59
60 if hc.Host == "" {
61 return false
62 }
63
64 return true
65 }
66
67 // Metrics contains the configuration for the Prometheus metrics
68 // collector.
69 type Metrics struct {
70 Host string `json:"host"`
71 Port string `json:"port"`
72 }
73
74 // Delegations contains configuration for persisting delegations.
75 type Delegations struct {
76 // Persist controls whether delegations are persisted or not.
77 Persist bool `json:"persist"`
78
79 // Policy contains the MSP predicate for delegation
80 // persistence, and users contains the users allowed
81 // to delegate.
82 Policy string `json:"policy"`
83 Users []string `json:"users"`
84
85 // Mechanism specifies the persistence mechanism to use.
86 Mechanism string `json:"mechanism"`
87
88 // Location contains location information for the persistence
89 // mechanism, such as a file path or database connection
90 // string.
91 Location string `json:"location"`
92 }
93
94 // Config contains all the configuration options for a redoctober
95 // instance.
96 type Config struct {
97 Server *Server `json:"server"`
98 UI *UI `json:"ui"`
99 HipChat *HipChat `json:"hipchat"`
100 Metrics *Metrics `json:"metrics"`
101 Delegations *Delegations `json:"delegations"`
102 }
103
104 // Valid ensures that the config has enough data to start a Red
105 // October process.
106 func (c *Config) Valid() bool {
107 // The RedOctober API relies on TLS for security.
108 if len(c.Server.CertPaths) == 0 || len(c.Server.KeyPaths) == 0 {
109 return false
110 }
111
112 // The server needs some address to listen on.
113 if c.Server.Addr == "" && !c.Server.Systemd {
114 return false
115 }
116
117 return true
118 }
119
120 // New returns a new, empty config.
121 func New() *Config {
122 return &Config{
123 Server: &Server{},
124 UI: &UI{},
125 HipChat: &HipChat{},
126 Metrics: &Metrics{},
127 Delegations: &Delegations{},
128 }
129 }
130
131 // Load reads a JSON-encoded config file from disk.
132 func Load(path string) (*Config, error) {
133 cfg := New()
134 in, err := ioutil.ReadFile(path)
135 if err != nil {
136 return nil, err
137 }
138
139 err = json.Unmarshal(in, cfg)
140 if err != nil {
141 return nil, err
142 }
143
144 return cfg, nil
145 }