@@ -39,23 +39,11 @@ func GetCertificates() func(http.ResponseWriter, *http.Request) {
39
39
// Route: GET /certificates/{certificateID}
40
40
func GetCertificate () func (http.ResponseWriter , * http.Request ) {
41
41
return func (w http.ResponseWriter , r * http.Request ) {
42
- var err error
43
- var certificateID int
44
- if certificateID , err = getURLParamInt (r , "certificateID" ); err != nil {
45
- h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
46
- return
47
- }
48
-
49
- item , err := certificate .GetByID (certificateID )
50
- switch err {
51
- case sql .ErrNoRows :
52
- h .NotFound (w , r )
53
- case nil :
42
+ logger .Debug ("here" )
43
+ if item := getCertificateFromRequest (w , r ); item != nil {
54
44
// nolint: errcheck,gosec
55
45
item .Expand (getExpandFromContext (r ))
56
46
h .ResultResponseJSON (w , r , http .StatusOK , item )
57
- default :
58
- h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
59
47
}
60
48
}
61
49
}
@@ -64,77 +52,40 @@ func GetCertificate() func(http.ResponseWriter, *http.Request) {
64
52
// Route: POST /certificates
65
53
func CreateCertificate () func (http.ResponseWriter , * http.Request ) {
66
54
return func (w http.ResponseWriter , r * http.Request ) {
67
- bodyBytes , _ := r .Context ().Value (c .BodyCtxKey ).([]byte )
68
-
69
- var newCertificate certificate.Model
70
- err := json .Unmarshal (bodyBytes , & newCertificate )
71
- if err != nil {
72
- h .ResultErrorJSON (w , r , http .StatusBadRequest , h .ErrInvalidPayload .Error (), nil )
73
- return
74
- }
75
-
76
- // Get userID from token
77
- userID , _ := r .Context ().Value (c .UserIDCtxKey ).(int )
78
- newCertificate .UserID = userID
55
+ var item certificate.Model
56
+ if fillObjectFromBody (w , r , "" , & item ) {
57
+ // Get userID from token
58
+ userID , _ := r .Context ().Value (c .UserIDCtxKey ).(int )
59
+ item .UserID = userID
60
+
61
+ if err := item .Save (); err != nil {
62
+ h .ResultErrorJSON (w , r , http .StatusBadRequest , fmt .Sprintf ("Unable to save Certificate: %s" , err .Error ()), nil )
63
+ return
64
+ }
79
65
80
- if err = newCertificate .Save (); err != nil {
81
- h .ResultErrorJSON (w , r , http .StatusBadRequest , fmt .Sprintf ("Unable to save Certificate: %s" , err .Error ()), nil )
82
- return
66
+ configureCertificate (item )
67
+ h .ResultResponseJSON (w , r , http .StatusOK , item )
83
68
}
84
-
85
- configureCertificate (newCertificate )
86
-
87
- h .ResultResponseJSON (w , r , http .StatusOK , newCertificate )
88
69
}
89
70
}
90
71
91
72
// UpdateCertificate updates a cert
92
73
// Route: PUT /certificates/{certificateID}
93
74
func UpdateCertificate () func (http.ResponseWriter , * http.Request ) {
94
75
return func (w http.ResponseWriter , r * http.Request ) {
95
- var err error
96
- var certificateID int
97
- if certificateID , err = getURLParamInt (r , "certificateID" ); err != nil {
98
- h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
99
- return
100
- }
101
-
102
- certificateObject , err := certificate .GetByID (certificateID )
103
- switch err {
104
- case sql .ErrNoRows :
105
- h .NotFound (w , r )
106
- case nil :
76
+ if item := getCertificateFromRequest (w , r ); item != nil {
107
77
// This is a special endpoint, as it needs to verify the schema payload
108
78
// based on the certificate type, without being given a type in the payload.
109
79
// The middleware would normally handle this.
110
- bodyBytes , _ := r .Context ().Value (c .BodyCtxKey ).([]byte )
111
- schemaErrors , jsonErr := middleware .CheckRequestSchema (r .Context (), schema .UpdateCertificate (certificateObject .Type ), bodyBytes )
112
- if jsonErr != nil {
113
- h .ResultErrorJSON (w , r , http .StatusInternalServerError , fmt .Sprintf ("Schema Fatal: %v" , jsonErr ), nil )
114
- return
115
- }
116
-
117
- if len (schemaErrors ) > 0 {
118
- h .ResultSchemaErrorJSON (w , r , schemaErrors )
119
- return
80
+ if fillObjectFromBody (w , r , schema .UpdateCertificate (item .Type ), item ) {
81
+ if err := item .Save (); err != nil {
82
+ h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
83
+ return
84
+ }
85
+
86
+ // configureCertificate(*item, item.Request)
87
+ h .ResultResponseJSON (w , r , http .StatusOK , item )
120
88
}
121
-
122
- err := json .Unmarshal (bodyBytes , & certificateObject )
123
- if err != nil {
124
- h .ResultErrorJSON (w , r , http .StatusBadRequest , h .ErrInvalidPayload .Error (), nil )
125
- return
126
- }
127
-
128
- if err = certificateObject .Save (); err != nil {
129
- h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
130
- return
131
- }
132
-
133
- configureCertificate (certificateObject )
134
-
135
- h .ResultResponseJSON (w , r , http .StatusOK , certificateObject )
136
- default :
137
- h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
138
89
}
139
90
}
140
91
}
@@ -143,31 +94,87 @@ func UpdateCertificate() func(http.ResponseWriter, *http.Request) {
143
94
// Route: DELETE /certificates/{certificateID}
144
95
func DeleteCertificate () func (http.ResponseWriter , * http.Request ) {
145
96
return func (w http.ResponseWriter , r * http.Request ) {
146
- var err error
147
- var certificateID int
148
- if certificateID , err = getURLParamInt (r , "certificateID" ); err != nil {
149
- h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
150
- return
151
- }
152
-
153
- item , err := certificate .GetByID (certificateID )
154
- switch err {
155
- case sql .ErrNoRows :
156
- h .NotFound (w , r )
157
- case nil :
158
- // Ensure that this upstream isn't in use by a host
159
- cnt := host .GetCertificateUseCount (certificateID )
97
+ if item := getCertificateFromRequest (w , r ); item != nil {
98
+ cnt := host .GetCertificateUseCount (item .ID )
160
99
if cnt > 0 {
161
100
h .ResultErrorJSON (w , r , http .StatusBadRequest , "Cannot delete certificate that is in use by at least 1 host" , nil )
162
101
return
163
102
}
164
103
h .ResultResponseJSON (w , r , http .StatusOK , item .Delete ())
165
- default :
166
- h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
167
104
}
168
105
}
169
106
}
170
107
108
+ // RenewCertificate is self explanatory
109
+ // Route: PUT /certificates/{certificateID}/renew
110
+ func RenewCertificate () func (http.ResponseWriter , * http.Request ) {
111
+ return func (w http.ResponseWriter , r * http.Request ) {
112
+ if item := getCertificateFromRequest (w , r ); item != nil {
113
+ configureCertificate (* item )
114
+ h .ResultResponseJSON (w , r , http .StatusOK , true )
115
+ }
116
+ }
117
+ }
118
+
119
+ // DownloadCertificate is self explanatory
120
+ // Route: PUT /certificates/{certificateID}/download
121
+ func DownloadCertificate () func (http.ResponseWriter , * http.Request ) {
122
+ return func (w http.ResponseWriter , r * http.Request ) {
123
+ if item := getCertificateFromRequest (w , r ); item != nil {
124
+ // todo
125
+ h .ResultResponseJSON (w , r , http .StatusOK , "ok" )
126
+ }
127
+ }
128
+ }
129
+
130
+ // getCertificateFromRequest has some reusable code for all endpoints that
131
+ // have a certificate id in the url. it will write errors to the output.
132
+ func getCertificateFromRequest (w http.ResponseWriter , r * http.Request ) * certificate.Model {
133
+ var err error
134
+ var certificateID int
135
+ if certificateID , err = getURLParamInt (r , "certificateID" ); err != nil {
136
+ h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
137
+ return nil
138
+ }
139
+
140
+ certificateObject , err := certificate .GetByID (certificateID )
141
+ switch err {
142
+ case sql .ErrNoRows :
143
+ h .NotFound (w , r )
144
+ case nil :
145
+ return & certificateObject
146
+ default :
147
+ h .ResultErrorJSON (w , r , http .StatusBadRequest , err .Error (), nil )
148
+ }
149
+ return nil
150
+ }
151
+
152
+ // getCertificateFromRequest has some reusable code for all endpoints that
153
+ // have a certificate id in the url. it will write errors to the output.
154
+ func fillObjectFromBody (w http.ResponseWriter , r * http.Request , validationSchema string , o interface {}) bool {
155
+ bodyBytes , _ := r .Context ().Value (c .BodyCtxKey ).([]byte )
156
+
157
+ if validationSchema != "" {
158
+ schemaErrors , jsonErr := middleware .CheckRequestSchema (r .Context (), validationSchema , bodyBytes )
159
+ if jsonErr != nil {
160
+ h .ResultErrorJSON (w , r , http .StatusInternalServerError , fmt .Sprintf ("Schema Fatal: %v" , jsonErr ), nil )
161
+ return false
162
+ }
163
+ if len (schemaErrors ) > 0 {
164
+ h .ResultSchemaErrorJSON (w , r , schemaErrors )
165
+ return false
166
+ }
167
+ }
168
+
169
+ err := json .Unmarshal (bodyBytes , o )
170
+ if err != nil {
171
+ h .ResultErrorJSON (w , r , http .StatusBadRequest , h .ErrInvalidPayload .Error (), nil )
172
+ return false
173
+ }
174
+
175
+ return true
176
+ }
177
+
171
178
func configureCertificate (c certificate.Model ) {
172
179
err := jobqueue .AddJob (jobqueue.Job {
173
180
Name : "RequestCertificate" ,
0 commit comments