/
resource.go
117 lines (93 loc) · 3.71 KB
/
resource.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cert
import (
"github.com/jumppad-labs/hclconfig/types"
"github.com/jumppad-labs/jumppad/pkg/config"
"github.com/jumppad-labs/jumppad/pkg/utils"
)
// TypeCertificateCA is the resource string for a self-signed CA
const TypeCertificateCA string = "certificate_ca"
// CertificateCA allows the generate of CA certificates
type CertificateCA struct {
types.ResourceMetadata `hcl:",remain"`
// Output directory to write the certificate and key too
Output string `hcl:"output" json:"output"`
// output parameters
// Key is the value related to the certificate key
PrivateKey File `hcl:"private_key,optional" json:"private_key"`
// Key is the value related to the certificate key
PublicKeyPEM File `hcl:"public_key_pem,optional" json:"public_key_pem"`
PublicKeySSH File `hcl:"public_key_ssh,optional" json:"public_key_ssh"`
// Cert is the value related to the certificate
Cert File `hcl:"certificate,optional" json:"certificate"`
}
func (c *CertificateCA) Process() error {
c.Output = utils.EnsureAbsolute(c.Output, c.ResourceFile)
c.PrivateKey = File{}
c.PublicKeySSH = File{}
c.PublicKeyPEM = File{}
c.Cert = File{}
// do we have an existing resource in the state?
// if so we need to set any computed resources for dependents
cfg, err := config.LoadState()
if err == nil {
// try and find the resource in the state
r, _ := cfg.FindResource(c.ResourceID)
if r != nil {
kstate := r.(*CertificateCA)
c.PrivateKey = kstate.PrivateKey
c.PublicKeySSH = kstate.PublicKeySSH
c.PublicKeyPEM = kstate.PublicKeyPEM
c.Cert = kstate.Cert
}
}
return nil
}
// TypeCertificateCA is the resource string for a self-signed CA
const TypeCertificateLeaf string = "certificate_leaf"
// CertificateCA allows the generate of CA certificates
type CertificateLeaf struct {
types.ResourceMetadata `hcl:",remain"`
CAKey string `hcl:"ca_key" json:"ca_key"` // Path to the primary key for the root CA
CACert string `hcl:"ca_cert" json:"ca_cert"` // Path to the root CA
IPAddresses []string `hcl:"ip_addresses,optional" json:"ip_addresses,omitempty"` // ip addresses to add to the cert
DNSNames []string `hcl:"dns_names,optional" json:"dns_names,omitempty"` // DNS names to add to the cert
Output string `hcl:"output" json:"output"` // output location for the certificate
// output parameters
// Key is the value related to the certificate key
PrivateKey File `hcl:"private_key,optional" json:"private_key"`
// Key is the value related to the certificate key
PublicKeyPEM File `hcl:"public_key_pem,optional" json:"public_key_pem"`
PublicKeySSH File `hcl:"public_key_ssh,optional" json:"public_key_ssh"`
// Cert is the value related to the certificate
Cert File `hcl:"certificate,optional" json:"certificate"`
}
func (c *CertificateLeaf) Process() error {
c.CACert = utils.EnsureAbsolute(c.CACert, c.ResourceFile)
c.CAKey = utils.EnsureAbsolute(c.CAKey, c.ResourceFile)
c.Output = utils.EnsureAbsolute(c.Output, c.ResourceFile)
c.PrivateKey = File{}
c.PublicKeySSH = File{}
c.PublicKeyPEM = File{}
c.Cert = File{}
// do we have an existing resource in the state?
// if so we need to set any computed resources for dependents
cfg, err := config.LoadState()
if err == nil {
// try and find the resource in the state
r, _ := cfg.FindResource(c.ResourceID)
if r != nil {
kstate := r.(*CertificateLeaf)
c.PrivateKey = kstate.PrivateKey
c.PublicKeySSH = kstate.PublicKeySSH
c.PublicKeyPEM = kstate.PublicKeyPEM
c.Cert = kstate.Cert
}
}
return nil
}
type File struct {
Filename string `hcl:"filename,optional" json:"filename"`
Directory string `hcl:"directory,optional" json:"directory"`
Path string `hcl:"path,optional" json:"path"`
Contents string `hcl:"contents,optional" json:"contents"`
}