/
enrollments.go
204 lines (169 loc) · 5.24 KB
/
enrollments.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cps
import (
"encoding/json"
"fmt"
"time"
client "github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
)
// Enrollments represents an enrollment
//
// API Docs: https://developer.akamai.com/api/core_features/certificate_provisioning_system/v2.html#enrollments
type Enrollment struct {
client.Resource
AdminContact *Contact `json:"adminContact"`
CertificateChainType *string `json:"certificateChainType"`
CertificateType CertificateType `json:"certificateType"`
CertificateSigningRequest *CSR `json:"csr"`
ChangeManagement bool `json:"changeManagement"`
EnableMultiStacked bool `json:"enableMultiStackedCertificates"`
Location *string `json:"location"`
MaxAllowedSans *int `json:"maxAllowedSanNames"`
MaxAllowedWildcardSans *int `json:"maxAllowedWildcardSanNames"`
NetworkConfiguration *NetworkConfiguration `json:"networkConfiguration"`
Organization *Organization `json:"org"`
PendingChanges *[]string `json:"pendingChanges"`
RegistrationAuthority RegistrationAuthority `json:"ra"`
SignatureAuthority *SHA `json:"signatureAlgorithm"`
TechContact *Contact `json:"techContact"`
ThirdParty *ThirdParty `json:"thirdParty"`
ValidationType ValidationType `json:"validationType"`
}
type CreateEnrollmentQueryParams struct {
ContractID string
DeployNotAfter *string
DeployNotBefore *string
}
type ListEnrollmentsQueryParams struct {
ContractID string
}
type CreateEnrollmentResponse struct {
Location string `json:"enrollment"`
Changes []string `json:"changes"`
}
func formatTime(t time.Time) string {
return fmt.Sprintf("%d-%02d-%02d", t.Year(), t.Month(), t.Day())
}
// Create an Enrollment on CPS
//
//
// API Docs: https://developer.akamai.com/api/core_features/certificate_provisioning_system/v2.html#5aaa335c
// Endpoint: POST /cps/v2/enrollments{?contractId,deploy-not-after,deploy-not-before}
func (enrollment *Enrollment) Create(params CreateEnrollmentQueryParams) (*CreateEnrollmentResponse, error) {
var request = fmt.Sprintf(
"/cps/v2/enrollments?contractId=%s",
params.ContractID,
)
if params.DeployNotAfter != nil {
request = fmt.Sprintf(
"%s&deploy-not-after=%s",
request,
*params.DeployNotAfter,
)
}
if params.DeployNotBefore != nil {
request = fmt.Sprintf(
"%s&deploy-not-before=%s",
request,
*params.DeployNotBefore,
)
}
req, err := newRequest(
"POST",
request,
enrollment,
)
if err != nil {
return nil, err
}
res, err := client.Do(Config, req)
if err != nil {
return nil, err
}
if client.IsError(res) {
return nil, client.NewAPIError(res)
}
var response CreateEnrollmentResponse
if err = client.BodyJSON(res, &response); err != nil {
return nil, err
}
return &response, nil
}
// Get an enrollment by location
//
//
// API Docs: https://developer.akamai.com/api/core_features/certificate_provisioning_system/v2.html#getasingleenrollment
// Endpoint: POST /cps/v2/enrollments/{enrollmentId}
func GetEnrollment(location string) (*Enrollment, error) {
req, err := client.NewRequest(
Config,
"GET",
location,
nil,
)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/vnd.akamai.cps.enrollment.v7+json")
res, err := client.Do(Config, req)
if err != nil {
return nil, err
}
if client.IsError(res) {
return nil, client.NewAPIError(res)
}
var response Enrollment
if err = client.BodyJSON(res, &response); err != nil {
return nil, err
}
return &response, nil
}
// ListEnrollments lists the names of each enrollment for the given contractID.
//
// API Docs: https://developer.akamai.com/api/core_features/certificate_provisioning_system/v2.html#getenrollments
// Endpoint: GET /cps/v2/enrollments{contractId}
func ListEnrollments(params ListEnrollmentsQueryParams) ([]Enrollment, error) {
// the returned JSON is a list in the enrollments parameter
enrollmentsResponse := struct {
Enrollments []Enrollment `json:"enrollments"`
}{}
req, err := client.NewRequest(
Config,
"GET",
fmt.Sprintf(
"/cps/v2/enrollments?contractId=%s",
params.ContractID,
),
nil,
)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/vnd.akamai.cps.enrollments.v7+json")
res, err := client.Do(Config, req)
if err != nil {
return nil, err
}
if client.IsError(res) {
return nil, client.NewAPIError(res)
}
if err = client.BodyJSON(res, &enrollmentsResponse); err != nil {
return nil, err
}
return enrollmentsResponse.Enrollments, nil
}
func (enrollment *Enrollment) Exists(enrollments []Enrollment) bool {
for _, e := range enrollments {
if e.CertificateSigningRequest.CommonName == enrollment.CertificateSigningRequest.CommonName {
return true
}
}
return false
}
// CreateEnrollment wraps enrollment.Create to accept json
func CreateEnrollment(data []byte, params CreateEnrollmentQueryParams) (*CreateEnrollmentResponse, error) {
var enrollment Enrollment
if err := json.Unmarshal(data, &enrollment); err != nil {
return nil, err
}
return enrollment.Create(params)
}