forked from openshift/assisted-service
/
manifests.go
504 lines (449 loc) · 19.5 KB
/
manifests.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package manifests
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/openshift/assisted-service/internal/common"
"github.com/openshift/assisted-service/internal/constants"
manifestsapi "github.com/openshift/assisted-service/internal/manifests/api"
"github.com/openshift/assisted-service/internal/usage"
"github.com/openshift/assisted-service/models"
"github.com/openshift/assisted-service/pkg/filemiddleware"
logutil "github.com/openshift/assisted-service/pkg/log"
"github.com/openshift/assisted-service/pkg/s3wrapper"
operations "github.com/openshift/assisted-service/restapi/operations/manifests"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
"gopkg.in/yaml.v2"
"gorm.io/gorm"
)
var _ manifestsapi.ManifestsAPI = &Manifests{}
// NewManifestsAPI returns manifests API
func NewManifestsAPI(db *gorm.DB, log logrus.FieldLogger, objectHandler s3wrapper.API, usageAPI usage.API) *Manifests {
return &Manifests{
db: db,
log: log,
objectHandler: objectHandler,
usageAPI: usageAPI,
}
}
// Manifests represents manifests handler implementation
type Manifests struct {
db *gorm.DB
log logrus.FieldLogger
objectHandler s3wrapper.API
usageAPI usage.API
}
func (m *Manifests) CreateClusterManifestInternal(ctx context.Context, params operations.V2CreateClusterManifestParams, isCustomManifest bool) (*models.Manifest, error) {
log := logutil.FromContext(ctx, m.log)
log.Infof("Creating manifest in cluster %s", params.ClusterID.String())
folder, fileName, path := m.getManifestPathsFromParameters(ctx, params.CreateManifestParams.Folder, params.CreateManifestParams.FileName)
log.Infof("Folder = '%s' and filename = '%s' and path = '%s'", folder, fileName, path)
// Verify that the manifests are created for a valid cluster
// to align kube-api and ocm behavior.
// In OCM, this is validated at the authorization layer. In other
// authorization scheme, it does not and therefore should be checked
// at the application level.
if _, err := common.GetClusterFromDB(m.db, params.ClusterID, false); err != nil {
return nil, common.NewApiError(http.StatusNotFound, fmt.Errorf("Object Not Found"))
}
err := m.validateManifestFileNames(ctx, params.ClusterID, []string{fileName})
if err != nil {
return nil, err
}
var manifestContent []byte
manifestContent, err = m.decodeUserSuppliedManifest(ctx, params.ClusterID, *params.CreateManifestParams.Content)
if err != nil {
return nil, err
}
err = m.validateUserSuppliedManifest(ctx, params.ClusterID, manifestContent, path)
if err != nil {
return nil, err
}
err = m.uploadManifest(ctx, manifestContent, params.ClusterID, path)
if err != nil {
return nil, err
}
if isCustomManifest {
err = m.markUserSuppliedManifest(ctx, params.ClusterID, path)
if err != nil {
return nil, err
}
}
log.Infof("Done creating manifest %s for cluster %s", path, params.ClusterID.String())
manifest := models.Manifest{FileName: fileName, Folder: folder}
return &manifest, nil
}
func (m *Manifests) unmarkUserSuppliedManifest(ctx context.Context, clusterID strfmt.UUID, path string) error {
objectName := GetManifestMetadataObjectName(clusterID, path, constants.ManifestSourceUserSupplied)
exists := false
var err error
if exists, err = m.objectHandler.DoesObjectExist(ctx, objectName); err != nil {
return errors.Wrapf(err, "Failed to delete metadata object %s from storage for cluster %s", objectName, clusterID)
}
if exists {
if _, err = m.objectHandler.DeleteObject(ctx, objectName); err != nil {
return errors.Wrapf(err, "Failed to delete metadata object %s from storage for cluster %s", objectName, clusterID)
}
}
return nil
}
func (m *Manifests) markUserSuppliedManifest(ctx context.Context, clusterID strfmt.UUID, path string) error {
objectName := GetManifestMetadataObjectName(clusterID, path, constants.ManifestSourceUserSupplied)
if err := m.objectHandler.Upload(ctx, []byte{}, objectName); err != nil {
return err
}
return nil
}
func (m *Manifests) IsUserManifest(ctx context.Context, clusterID strfmt.UUID, folder string, fileName string) (bool, error) {
path := filepath.Join(folder, fileName)
isCustomManifest, err := m.objectHandler.DoesObjectExist(ctx, GetManifestMetadataObjectName(clusterID, path, constants.ManifestSourceUserSupplied))
return isCustomManifest, err
}
func IsManifest(file string) bool {
parts := strings.Split(strings.Trim(file, string(filepath.Separator)), string(filepath.Separator))
return len(parts) > 2 && parts[1] == models.ManifestFolderManifests
}
func ParsePath(file string) (folder string, filename string, err error) {
parts := strings.Split(strings.Trim(file, string(filepath.Separator)), string(filepath.Separator))
if !(len(parts) > 2 && parts[1] == "manifests") {
return "", "", errors.Errorf("Filepath %s is not a manifest path", file)
}
return parts[2], parts[3], nil
}
func (m *Manifests) ListClusterManifestsInternal(ctx context.Context, params operations.V2ListClusterManifestsParams) (models.ListManifests, error) {
log := logutil.FromContext(ctx, m.log)
log.Debugf("Listing manifests in cluster %s", params.ClusterID)
// Verify that the manifests are created for a valid cluster
// to align kube-api and ocm behavior.
// In OCM, this is validated at the authorization layer. In other
// authorization scheme, it does not and therefore should be checked
// at the application level.
if _, err := common.GetClusterFromDB(m.db, params.ClusterID, false); err != nil {
return nil, common.NewApiError(http.StatusNotFound, fmt.Errorf("Object Not Found"))
}
objectName := filepath.Join(params.ClusterID.String(), constants.ManifestFolder)
files, err := m.objectHandler.ListObjectsByPrefix(ctx, objectName)
if err != nil {
return nil, common.NewApiError(http.StatusInternalServerError, err)
}
manifests := models.ListManifests{}
for _, file := range files {
folder, filename, err := ParsePath(file)
if err != nil {
return nil, err
}
isUserManifest, err := m.IsUserManifest(ctx, params.ClusterID, folder, filename)
if err != nil {
return nil, err
}
if isUserManifest {
manifests = append(manifests, &models.Manifest{FileName: filename, Folder: folder})
} else {
return nil, common.NewApiError(http.StatusInternalServerError, errors.Errorf("Cannot list file %s in cluster %s", file, params.ClusterID.String()))
}
}
return manifests, nil
}
func (m *Manifests) DeleteClusterManifestInternal(ctx context.Context, params operations.V2DeleteClusterManifestParams) error {
log := logutil.FromContext(ctx, m.log)
log.Infof("Deleting manifest from cluster %s", params.ClusterID.String())
cluster, err := common.GetClusterFromDB(m.db, params.ClusterID, common.SkipEagerLoading)
if err != nil {
return common.NewApiError(http.StatusNotFound, fmt.Errorf("Object Not Found"))
}
err = m.validateAllowedToModifyManifests(ctx, cluster)
if err != nil {
log.WithError(err).Errorf("Not allowed to modify manifest for cluster id %s", params.ClusterID.String())
return err
}
_, _, path := m.getManifestPathsFromParameters(ctx, params.Folder, ¶ms.FileName)
err = m.deleteManifest(ctx, params.ClusterID, path)
if err != nil {
return err
}
log.Infof("Done deleting cluster manifest %s for cluster %s", path, params.ClusterID.String())
return nil
}
func (m *Manifests) UpdateClusterManifestInternal(ctx context.Context, params operations.V2UpdateClusterManifestParams) (*models.Manifest, error) {
if params.UpdateManifestParams.UpdatedFolder == nil {
params.UpdateManifestParams.UpdatedFolder = ¶ms.UpdateManifestParams.Folder
}
if params.UpdateManifestParams.UpdatedFileName == nil {
params.UpdateManifestParams.UpdatedFileName = ¶ms.UpdateManifestParams.FileName
}
srcFolder, srcFileName, srcPath := m.getManifestPathsFromParameters(ctx, ¶ms.UpdateManifestParams.Folder, ¶ms.UpdateManifestParams.FileName)
destFolder, destFileName, destPath := m.getManifestPathsFromParameters(ctx, params.UpdateManifestParams.UpdatedFolder, params.UpdateManifestParams.UpdatedFileName)
cluster, err := common.GetClusterFromDB(m.db, params.ClusterID, common.SkipEagerLoading)
if err != nil {
err = fmt.Errorf("Object Not Found")
m.log.Infof(err.Error())
return nil, common.NewApiError(http.StatusNotFound, err)
}
err = m.validateAllowedToModifyManifests(ctx, cluster)
if err != nil {
return nil, err
}
err = m.validateManifestFileNames(ctx, params.ClusterID, []string{srcFileName, destFileName})
if err != nil {
return nil, err
}
var content []byte
if params.UpdateManifestParams.UpdatedContent != nil {
content, err = m.decodeUserSuppliedManifest(ctx, params.ClusterID, *params.UpdateManifestParams.UpdatedContent)
if err != nil {
return nil, err
}
err = m.validateUserSuppliedManifest(ctx, params.ClusterID, content, srcFileName)
if err != nil {
return nil, err
}
} else {
content, err = m.fetchManifestContent(ctx, params.ClusterID, srcFolder, srcFileName)
if err != nil {
return nil, err
}
}
err = m.uploadManifest(ctx, content, params.ClusterID, destPath)
if err != nil {
return nil, err
}
isCustomManifest, err := m.IsUserManifest(ctx, params.ClusterID, srcFolder, srcFileName)
if err != nil {
return nil, err
}
if isCustomManifest {
err = m.markUserSuppliedManifest(ctx, params.ClusterID, destPath)
if err != nil {
return nil, err
}
}
if srcPath != destPath {
err = m.deleteManifest(ctx, params.ClusterID, srcPath)
if err != nil {
return nil, err
}
}
manifest := models.Manifest{FileName: destFileName, Folder: destFolder}
return &manifest, nil
}
func (m *Manifests) V2DownloadClusterManifest(ctx context.Context, params operations.V2DownloadClusterManifestParams) middleware.Responder {
log := logutil.FromContext(ctx, m.log)
if params.Folder == nil {
defaultFolder := models.CreateManifestParamsFolderManifests
params.Folder = &defaultFolder
}
_, fileName, path := m.getManifestPathsFromParameters(ctx, params.Folder, ¶ms.FileName)
// Verify that the manifests are created for a valid cluster
// to align kube-api and ocm behavior.
// In OCM, this is validated at the authorization layer. In other
// authorization scheme, it does not and therefore should be checked
// at the application level.
if _, err := common.GetClusterFromDB(m.db, params.ClusterID, false); err != nil {
return common.NewApiError(http.StatusNotFound, fmt.Errorf("Object Not Found"))
}
objectName := GetManifestObjectName(params.ClusterID, path)
exists, err := m.objectHandler.DoesObjectExist(ctx, objectName)
if err != nil {
log.WithError(err).Errorf("Failed to download cluster manifest")
return common.GenerateErrorResponder(err)
}
if !exists {
msg := fmt.Sprintf("Cluster manifest %s doesn't exist in cluster %s", path, params.ClusterID.String())
log.Warn(msg)
return common.GenerateErrorResponderWithDefault(errors.New(msg), http.StatusNotFound)
}
respBody, contentLength, err := m.objectHandler.Download(ctx, objectName)
if err != nil {
log.WithError(err).Errorf("failed to download file %s from cluster: %s", fileName, params.ClusterID.String())
return common.GenerateErrorResponder(err)
}
return filemiddleware.NewResponder(operations.NewV2DownloadClusterManifestOK().WithPayload(respBody), fileName, contentLength, nil)
}
func (m *Manifests) setUsage(active bool, manifest *models.Manifest, clusterID strfmt.UUID) error {
err := m.db.Transaction(func(tx *gorm.DB) error {
cluster, err := common.GetClusterFromDB(tx, clusterID, common.SkipEagerLoading)
if err != nil {
return err
}
if usages, uerr := usage.Unmarshal(cluster.Cluster.FeatureUsage); uerr == nil {
if active {
m.usageAPI.Add(usages, usage.CustomManifest, nil)
} else {
m.usageAPI.Remove(usages, usage.CustomManifest)
}
m.usageAPI.Save(tx, clusterID, usages)
}
return nil
})
return err
}
// GetManifestObjectName returns the manifest object name as stored in S3
func GetManifestObjectName(clusterID strfmt.UUID, fileName string) string {
return filepath.Join(string(clusterID), constants.ManifestFolder, fileName)
}
// GetManifestObjectName returns the manifest object name as stored in S3
func GetManifestMetadataObjectName(clusterID strfmt.UUID, fileName string, manifestSource string) string {
return filepath.Join(string(clusterID), constants.ManifestMetadataFolder, fileName, manifestSource)
}
// GetClusterManifests returns a list of cluster manifests
func GetClusterManifests(ctx context.Context, clusterID *strfmt.UUID, s3Client s3wrapper.API) ([]string, error) {
manifestFiles := []string{}
files, err := listManifests(ctx, clusterID, models.CreateManifestParamsFolderManifests, s3Client)
if err != nil {
return []string{}, err
}
manifestFiles = append(manifestFiles, files...)
files, err = listManifests(ctx, clusterID, models.CreateManifestParamsFolderOpenshift, s3Client)
if err != nil {
return []string{}, err
}
manifestFiles = append(manifestFiles, files...)
return manifestFiles, nil
}
func listManifests(ctx context.Context, clusterID *strfmt.UUID, folder string, s3Client s3wrapper.API) ([]string, error) {
key := GetManifestObjectName(*clusterID, folder)
files, err := s3Client.ListObjectsByPrefix(ctx, key)
if err != nil {
return []string{}, err
}
return files, nil
}
func (m *Manifests) prepareAndLogError(ctx context.Context, httpStatusCode int32, err error) error {
log := logutil.FromContext(ctx, m.log)
log.Error(err)
return common.NewApiError(httpStatusCode, err)
}
func (m *Manifests) fetchManifestContent(ctx context.Context, clusterID strfmt.UUID, folderName string, fileName string) ([]byte, error) {
path := filepath.Join(folderName, fileName)
respBody, _, err := m.objectHandler.Download(ctx, GetManifestObjectName(clusterID, path))
if err != nil {
return nil, m.prepareAndLogError(ctx, http.StatusInternalServerError, errors.Wrapf(err, "Failed to fetch content from %s for cluster %s", path, clusterID))
}
content, err := ioutil.ReadAll(respBody)
if err != nil {
return nil, m.prepareAndLogError(ctx, http.StatusInternalServerError, errors.Wrapf(err, "Failed fetch response body from %s for cluster %s", path, clusterID))
}
return content, nil
}
func (m *Manifests) validateManifestFileNames(ctx context.Context, clusterID strfmt.UUID, fileNames []string) error {
for _, fileName := range fileNames {
if strings.Contains(fileName, " ") {
return m.prepareAndLogError(
ctx,
http.StatusBadRequest,
errors.Errorf("Cluster manifest %s for cluster %s should not include a space in its name.",
fileName,
clusterID))
}
if strings.ContainsRune(fileName, os.PathSeparator) {
return m.prepareAndLogError(
ctx,
http.StatusBadRequest,
errors.Errorf("Cluster manifest %s for cluster %s should not include a directory in its name.",
fileName,
clusterID))
}
}
return nil
}
func (m *Manifests) validateAllowedToModifyManifests(ctx context.Context, cluster *common.Cluster) error {
// Creation / deletion/ alteration of manifests is not allowed after installation has started.
preInstallationStates := []string{
models.ClusterStatusPendingForInput,
models.ClusterStatusInsufficient,
models.ClusterStatusReady,
}
if !funk.ContainsString(preInstallationStates, swag.StringValue(cluster.Status)) {
return m.prepareAndLogError(ctx, http.StatusBadRequest, errors.Errorf("cluster %s is not in pre-installation states, "+
"can't modify manifests after installation has been started",
cluster.ID))
}
return nil
}
func (m *Manifests) validateUserSuppliedManifest(ctx context.Context, clusterID strfmt.UUID, manifestContent []byte, fileName string) error {
// etcd resources in k8s are limited to 1.5 MiB as indicated here https://etcd.io/docs/v3.5/dev-guide/limit/#request-size-limit
// however, one the the resource types that can be created from a manifest is a ConfigMap
// which has a size limit of 1MiB as cited here https://kubernetes.io/docs/concepts/configuration/configmap
// so this limit has been chosen based on the lowest permitted resource size (the size of the ConfigMap)
maxFileSizeBytes := 1024 * 1024
if len(manifestContent) > maxFileSizeBytes {
return m.prepareAndLogError(ctx, http.StatusBadRequest, errors.Errorf("Manifest content of file %s for cluster ID %s exceeds the maximum file size of 1MiB", fileName, string(clusterID)))
}
extension := filepath.Ext(fileName)
if extension == ".yaml" || extension == ".yml" {
var s map[interface{}]interface{}
if yaml.Unmarshal(manifestContent, &s) != nil {
return m.prepareAndLogError(ctx, http.StatusBadRequest, errors.Errorf("Manifest content of file %s for cluster ID %s has an invalid YAML format", fileName, string(clusterID)))
}
} else if extension == ".json" {
if !json.Valid(manifestContent) {
return m.prepareAndLogError(ctx, http.StatusBadRequest, errors.Errorf("Manifest content of file %s for cluster ID %s has an illegal JSON format", fileName, string(clusterID)))
}
} else if strings.HasPrefix(extension, ".patch") && (strings.Contains(fileName, ".yaml.patch") || strings.Contains(fileName, ".yml.patch")) {
var s []map[interface{}]interface{}
if yaml.Unmarshal(manifestContent, &s) != nil {
return m.prepareAndLogError(ctx, http.StatusBadRequest, errors.Errorf("Patch content of file %s for cluster ID %s has an invalid YAML format", fileName, string(clusterID)))
}
} else {
return m.prepareAndLogError(ctx, http.StatusBadRequest, errors.Errorf("Manifest filename of file %s for cluster ID %s is invalid. Only json, yaml and yml extensions are supported", fileName, string(clusterID)))
}
return nil
}
func (m *Manifests) decodeUserSuppliedManifest(ctx context.Context, clusterID strfmt.UUID, manifest string) ([]byte, error) {
manifestContent, err := base64.StdEncoding.DecodeString(manifest)
if err != nil {
return nil, m.prepareAndLogError(ctx, http.StatusBadRequest, errors.Errorf("failed to base64-decode cluster manifest content for cluster %s", string(clusterID)))
}
return manifestContent, nil
}
func (m *Manifests) getManifestPathsFromParameters(ctx context.Context, folder *string, fileName *string) (string, string, string) {
if folder == nil {
defaultFolder := models.CreateManifestParamsFolderManifests
folder = &defaultFolder
}
return *folder, *fileName, filepath.Join(*folder, *fileName)
}
func (m *Manifests) uploadManifest(ctx context.Context, content []byte, clusterID strfmt.UUID, path string) error {
objectName := GetManifestObjectName(clusterID, path)
if err := m.objectHandler.Upload(ctx, content, objectName); err != nil {
return m.prepareAndLogError(ctx, http.StatusInternalServerError, errors.Wrapf(err, "Failed to upload mainfest object %s for cluster %s", objectName, clusterID))
}
return nil
}
func (m *Manifests) deleteManifest(ctx context.Context, clusterID strfmt.UUID, path string) error {
log := logutil.FromContext(ctx, m.log)
objectName := GetManifestObjectName(clusterID, path)
exists, err := m.objectHandler.DoesObjectExist(ctx, objectName)
if err != nil {
return m.prepareAndLogError(
ctx,
http.StatusInternalServerError,
errors.Wrapf(err, "There was an error while determining the existence of manifest %s for cluster %s", path, clusterID))
}
if !exists {
log.Infof("Cluster manifest %s doesn't exists for cluster %s", path, clusterID)
return nil
}
if _, err = m.objectHandler.DeleteObject(ctx, objectName); err != nil {
return m.prepareAndLogError(
ctx,
http.StatusInternalServerError,
errors.Wrapf(err, "Failed to delete object %s from storage for cluster %s", objectName, clusterID))
}
err = m.unmarkUserSuppliedManifest(ctx, clusterID, path)
if err != nil {
return err
}
return nil
}