|
1 |
| -package api |
2 |
| - |
3 |
| -import ( |
4 |
| - "crypto/tls" |
5 |
| - "crypto/x509" |
6 |
| - "fmt" |
7 |
| - "os" |
8 |
| - |
9 |
| - "github.com/hashicorp/hcl/hcl/ast" |
10 |
| -) |
11 |
| - |
12 |
| -// TornjakServerInfo provides insight into the configuration of the SPIRE server |
13 |
| -// where the Tornjak Agent resides |
14 |
| -type TornjakSpireServerInfo struct { |
15 |
| - // Plugins is a map from plugin types to respective names of plugins configured |
16 |
| - Plugins map[string][]string `json:"plugins"` |
17 |
| - // TrustDomain specifies the trust domain of the SPIRE server configured with tornjak |
18 |
| - TrustDomain string `json:"trustDomain"` |
19 |
| - // Verbose config contains unstructure information on the config on the agent |
20 |
| - VerboseConfig string `json:"verboseConfig"` |
21 |
| -} |
22 |
| - |
23 |
| -// pared down version of full Server Config type spire/cmd/spire-server/cli/run |
24 |
| -// we curently need only extract the trust domain |
25 |
| -type SpireServerConfig struct { |
26 |
| - TrustDomain string `hcl:"trust_domain"` |
27 |
| -} |
28 |
| - |
29 |
| -type SPIREConfig struct { |
30 |
| - Server *SpireServerConfig `hcl:"server"` |
31 |
| - Plugins ast.Node `hcl:"plugins"` |
32 |
| -} |
33 |
| - |
34 |
| -type TornjakConfig struct { |
35 |
| - Server *serverConfig `hcl:"server"` |
36 |
| - Plugins *ast.Node `hcl:"plugins"` |
37 |
| -} |
38 |
| - |
39 |
| -/* Server configuration*/ |
40 |
| - |
41 |
| -type serverConfig struct { |
42 |
| - SPIRESocket string `hcl:"spire_socket_path"` |
43 |
| - HTTPConfig *HTTPConfig `hcl:"http"` |
44 |
| - HTTPSConfig *HTTPSConfig `hcl:"https"` |
45 |
| -} |
46 |
| - |
47 |
| -type HTTPConfig struct { |
48 |
| - ListenPort int `hcl:"port"` |
49 |
| -} |
50 |
| - |
51 |
| -type HTTPSConfig struct { |
52 |
| - ListenPort int `hcl:"port"` |
53 |
| - Cert string `hcl:"cert"` |
54 |
| - Key string `hcl:"key"` |
55 |
| - ClientCA string `hcl:"client_ca"` |
56 |
| -} |
57 |
| - |
58 |
| -func (h HTTPSConfig) Parse() (*tls.Config, error) { |
59 |
| - serverCertPath := h.Cert |
60 |
| - serverKeyPath := h.Key |
61 |
| - clientCAPath := h.ClientCA |
62 |
| - |
63 |
| - mtls := (clientCAPath != "") |
64 |
| - |
65 |
| - if _, err := os.Stat(serverCertPath); os.IsNotExist(err) { |
66 |
| - return nil, fmt.Errorf("server cert path '%s': %w", serverCertPath, err) |
67 |
| - } |
68 |
| - if _, err := os.Stat(serverKeyPath); os.IsNotExist(err) { |
69 |
| - return nil, fmt.Errorf("server key path '%s': %w", serverKeyPath, err) |
70 |
| - } |
71 |
| - |
72 |
| - // Create a CA certificate pool and add cert.pem to it |
73 |
| - serverCert, err := os.ReadFile(serverCertPath) |
74 |
| - if err != nil { |
75 |
| - return nil, fmt.Errorf("server ca pool error: %w", err) |
76 |
| - } |
77 |
| - caCertPool := x509.NewCertPool() |
78 |
| - caCertPool.AppendCertsFromPEM(serverCert) |
79 |
| - |
80 |
| - if mtls { |
81 |
| - // add mTLS CA path to cert pool as well |
82 |
| - if _, err := os.Stat(clientCAPath); os.IsNotExist(err) { |
83 |
| - return nil, fmt.Errorf("server file does not exist %s", clientCAPath) |
84 |
| - } |
85 |
| - clientCA, err := os.ReadFile(clientCAPath) |
86 |
| - if err != nil { |
87 |
| - return nil, fmt.Errorf("server: could not read file %s: %w", clientCAPath, err) |
88 |
| - } |
89 |
| - caCertPool.AppendCertsFromPEM(clientCA) |
90 |
| - } |
91 |
| - |
92 |
| - // Create the TLS Config with the CA pool and enable Client certificate validation |
93 |
| - tlsConfig := &tls.Config{ |
94 |
| - ClientCAs: caCertPool, |
95 |
| - } |
96 |
| - |
97 |
| - if mtls { |
98 |
| - tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert |
99 |
| - } |
100 |
| - //tlsConfig.BuildNameToCertificate() |
101 |
| - |
102 |
| - return tlsConfig, nil |
103 |
| -} |
104 |
| - |
105 |
| -/* Plugin types */ |
106 |
| -type pluginDataStoreSQL struct { |
107 |
| - Drivername string `hcl:"drivername"` |
108 |
| - Filename string `hcl:"filename"` |
109 |
| -} |
110 |
| - |
111 |
| -type pluginAuthenticatorKeycloak struct { |
112 |
| - IssuerURL string `hcl:"issuer"` |
113 |
| - Audience string `hcl:"audience"` |
114 |
| -} |
115 |
| - |
116 |
| -type AuthRole struct { |
117 |
| - Name string `hcl:",key"` |
118 |
| - Desc string `hcl:"desc"` |
119 |
| -} |
120 |
| - |
121 |
| -type APIv1RoleMapping struct { |
122 |
| - Name string `hcl:",key"` |
123 |
| - Method string `hcl:"-"` |
124 |
| - Path string `hcl:"-"` |
125 |
| - AllowedRoles []string `hcl:"allowed_roles"` |
126 |
| -} |
127 |
| - |
128 |
| -type pluginAuthorizerRBAC struct { |
129 |
| - Name string `hcl:"name"` |
130 |
| - RoleList []*AuthRole `hcl:"role,block"` |
131 |
| - APIv1RoleMappings []*APIv1RoleMapping `hcl:"APIv1,block"` |
132 |
| -} |
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/tls" |
| 5 | + "crypto/x509" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + "github.com/hashicorp/hcl/hcl/ast" |
| 10 | +) |
| 11 | + |
| 12 | +// TornjakServerInfo provides insight into the configuration of the SPIRE server |
| 13 | +// where the Tornjak Agent resides |
| 14 | +type TornjakSpireServerInfo struct { |
| 15 | + // Plugins is a map from plugin types to respective names of plugins configured |
| 16 | + Plugins map[string][]string `json:"plugins"` |
| 17 | + // TrustDomain specifies the trust domain of the SPIRE server configured with tornjak |
| 18 | + TrustDomain string `json:"trustDomain"` |
| 19 | + // Verbose config contains unstructure information on the config on the agent |
| 20 | + VerboseConfig string `json:"verboseConfig"` |
| 21 | +} |
| 22 | + |
| 23 | +// pared down version of full Server Config type spire/cmd/spire-server/cli/run |
| 24 | +// we curently need only extract the trust domain |
| 25 | +type SpireServerConfig struct { |
| 26 | + TrustDomain string `hcl:"trust_domain"` |
| 27 | +} |
| 28 | + |
| 29 | +type SPIREConfig struct { |
| 30 | + Server *SpireServerConfig `hcl:"server"` |
| 31 | + Plugins ast.Node `hcl:"plugins"` |
| 32 | +} |
| 33 | + |
| 34 | +type TornjakConfig struct { |
| 35 | + Server *serverConfig `hcl:"server"` |
| 36 | + Plugins *ast.Node `hcl:"plugins"` |
| 37 | +} |
| 38 | + |
| 39 | +/* Server configuration*/ |
| 40 | + |
| 41 | +type serverConfig struct { |
| 42 | + SPIRESocket string `hcl:"spire_socket_path"` |
| 43 | + HTTPConfig *HTTPConfig `hcl:"http"` |
| 44 | + HTTPSConfig *HTTPSConfig `hcl:"https"` |
| 45 | +} |
| 46 | + |
| 47 | +type HTTPConfig struct { |
| 48 | + ListenPort int `hcl:"port"` |
| 49 | +} |
| 50 | + |
| 51 | +type HTTPSConfig struct { |
| 52 | + ListenPort int `hcl:"port"` |
| 53 | + Cert string `hcl:"cert"` |
| 54 | + Key string `hcl:"key"` |
| 55 | + ClientCA string `hcl:"client_ca"` |
| 56 | +} |
| 57 | + |
| 58 | +func (h HTTPSConfig) Parse() (*tls.Config, error) { |
| 59 | + serverCertPath := h.Cert |
| 60 | + serverKeyPath := h.Key |
| 61 | + clientCAPath := h.ClientCA |
| 62 | + |
| 63 | + mtls := (clientCAPath != "") |
| 64 | + |
| 65 | + if _, err := os.Stat(serverCertPath); os.IsNotExist(err) { |
| 66 | + return nil, fmt.Errorf("server cert path '%s': %w", serverCertPath, err) |
| 67 | + } |
| 68 | + if _, err := os.Stat(serverKeyPath); os.IsNotExist(err) { |
| 69 | + return nil, fmt.Errorf("server key path '%s': %w", serverKeyPath, err) |
| 70 | + } |
| 71 | + |
| 72 | + // Create a CA certificate pool and add cert.pem to it |
| 73 | + serverCert, err := os.ReadFile(serverCertPath) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("server ca pool error: %w", err) |
| 76 | + } |
| 77 | + caCertPool := x509.NewCertPool() |
| 78 | + caCertPool.AppendCertsFromPEM(serverCert) |
| 79 | + |
| 80 | + if mtls { |
| 81 | + // add mTLS CA path to cert pool as well |
| 82 | + if _, err := os.Stat(clientCAPath); os.IsNotExist(err) { |
| 83 | + return nil, fmt.Errorf("server file does not exist %s", clientCAPath) |
| 84 | + } |
| 85 | + clientCA, err := os.ReadFile(clientCAPath) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("server: could not read file %s: %w", clientCAPath, err) |
| 88 | + } |
| 89 | + caCertPool.AppendCertsFromPEM(clientCA) |
| 90 | + } |
| 91 | + |
| 92 | + // Create the TLS Config with the CA pool and enable Client certificate validation |
| 93 | + tlsConfig := &tls.Config{ |
| 94 | + ClientCAs: caCertPool, |
| 95 | + } |
| 96 | + |
| 97 | + if mtls { |
| 98 | + tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert |
| 99 | + } |
| 100 | + //tlsConfig.BuildNameToCertificate() |
| 101 | + |
| 102 | + return tlsConfig, nil |
| 103 | +} |
| 104 | + |
| 105 | +/* Plugin types */ |
| 106 | +type pluginDataStoreSQL struct { |
| 107 | + Drivername string `hcl:"drivername"` |
| 108 | + Filename string `hcl:"filename"` |
| 109 | +} |
| 110 | + |
| 111 | +type pluginControllerManager struct { |
| 112 | + Classname string `hcl:"classname"` |
| 113 | +} |
| 114 | + |
| 115 | +type pluginAuthenticatorKeycloak struct { |
| 116 | + IssuerURL string `hcl:"issuer"` |
| 117 | + Audience string `hcl:"audience"` |
| 118 | +} |
| 119 | + |
| 120 | +type AuthRole struct { |
| 121 | + Name string `hcl:",key"` |
| 122 | + Desc string `hcl:"desc"` |
| 123 | +} |
| 124 | + |
| 125 | +type APIv1RoleMapping struct { |
| 126 | + Name string `hcl:",key"` |
| 127 | + Method string `hcl:"-"` |
| 128 | + Path string `hcl:"-"` |
| 129 | + AllowedRoles []string `hcl:"allowed_roles"` |
| 130 | +} |
| 131 | + |
| 132 | +type pluginAuthorizerRBAC struct { |
| 133 | + Name string `hcl:"name"` |
| 134 | + RoleList []*AuthRole `hcl:"role,block"` |
| 135 | + APIv1RoleMappings []*APIv1RoleMapping `hcl:"APIv1,block"` |
| 136 | +} |
0 commit comments