1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { AbstractControl, Validators } from '@angular/forms';
3 import { Router } from '@angular/router';
5 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
6 import _ from 'lodash';
8 import { RgwBucketService } from '~/app/shared/api/rgw-bucket.service';
9 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
10 import { NotificationType } from '~/app/shared/enum/notification-type.enum';
11 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
12 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
13 import { CdValidators } from '~/app/shared/forms/cd-validators';
14 import { NotificationService } from '~/app/shared/services/notification.service';
15 import { RgwBucketEncryptionModel } from '../models/rgw-bucket-encryption';
18 selector: 'cd-rgw-config-modal',
19 templateUrl: './rgw-config-modal.component.html',
20 styleUrls: ['./rgw-config-modal.component.scss'],
21 providers: [RgwBucketEncryptionModel]
23 export class RgwConfigModalComponent implements OnInit {
24 readonly vaultAddress = /^((https?:\/\/)|(www.))(?:([a-zA-Z]+)|(\d+\.\d+.\d+.\d+)):\d{4}$/;
26 kmsProviders: string[];
28 configForm: CdFormGroup;
31 submitAction = new EventEmitter();
32 authMethods: string[];
33 secretEngines: string[];
36 private formBuilder: CdFormBuilder,
37 public activeModal: NgbActiveModal,
38 private router: Router,
39 public actionLabels: ActionLabelsI18n,
40 private rgwBucketService: RgwBucketService,
41 private rgwEncryptionModal: RgwBucketEncryptionModel,
42 private notificationService: NotificationService
47 this.kmsProviders = this.rgwEncryptionModal.kmsProviders;
48 this.authMethods = this.rgwEncryptionModal.authMethods;
49 this.secretEngines = this.rgwEncryptionModal.secretEngines;
53 this.configForm = this.formBuilder.group({
58 CdValidators.custom('vaultPattern', (value: string) => {
59 if (_.isEmpty(value)) {
62 return !this.vaultAddress.test(value);
66 kms_provider: ['vault', Validators.required],
67 encryptionType: ['aws:kms', Validators.required],
68 auth_method: ['token', Validators.required],
69 secret_engine: ['kv', Validators.required],
75 CdValidators.requiredIf({
80 ssl_cert: [null, CdValidators.sslCert()],
81 client_cert: [null, CdValidators.pemCert()],
82 client_key: [null, CdValidators.sslPrivKey()],
83 kmsEnabled: [{ value: false }],
84 s3Enabled: [{ value: false }]
88 fileUpload(files: FileList, controlName: string) {
89 const file: File = files[0];
90 const reader = new FileReader();
91 reader.addEventListener('load', () => {
92 const control: AbstractControl = this.configForm.get(controlName);
93 control.setValue(file);
94 control.markAsDirty();
95 control.markAsTouched();
96 control.updateValueAndValidity();
101 const values = this.configForm.value;
102 this.rgwBucketService
103 .setEncryptionConfig(
104 values['encryptionType'],
105 values['kms_provider'],
106 values['auth_method'],
107 values['secret_engine'],
108 values['secret_path'],
114 values['client_cert'],
119 this.notificationService.show(
120 NotificationType.success,
121 $localize`Updated RGW Encryption Configuration values`
124 error: (error: any) => {
125 this.notificationService.show(NotificationType.error, error);
126 this.configForm.setErrors({ cdSubmitButton: true });
129 this.activeModal.close();
130 this.router.routeReuseStrategy.shouldReuseRoute = () => false;
131 this.router.onSameUrlNavigation = 'reload';
132 this.router.navigate([this.router.url]);