|
| 1 | +/** |
| 2 | + * Copyright IBM Corp. 2016, 2026 |
| 3 | + * SPDX-License-Identifier: BUSL-1.1 |
| 4 | + */ |
| 5 | + |
| 6 | +import Component from '@glimmer/component'; |
| 7 | +import { service } from '@ember/service'; |
| 8 | +import { action } from '@ember/object'; |
| 9 | +import { tracked } from '@glimmer/tracking'; |
| 10 | +import { task } from 'ember-concurrency'; |
| 11 | +import { waitFor } from '@ember/test-waiters'; |
| 12 | +import SshSignForm from 'vault/forms/ssh/sign'; |
| 13 | + |
| 14 | +import type ApiService from 'vault/services/api'; |
| 15 | +import type ControlGroupService from 'vault/vault/services/control-group'; |
| 16 | + |
| 17 | +interface Args { |
| 18 | + roleName: string; |
| 19 | + backendPath: string; |
| 20 | +} |
| 21 | + |
| 22 | +export default class SshSignKey extends Component<Args> { |
| 23 | + @service declare readonly api: ApiService; |
| 24 | + @service declare readonly controlGroup: ControlGroupService; |
| 25 | + |
| 26 | + @tracked signForm = new SshSignForm({ cert_type: 'user' }); |
| 27 | + @tracked signedKeyData: Record<string, unknown> | null = null; |
| 28 | + @tracked errorMessage: string | null = null; |
| 29 | + @tracked modelValidations: Record<string, unknown> | null = null; |
| 30 | + @tracked invalidFormAlert: string | null = null; |
| 31 | + |
| 32 | + get signDisplayRows() { |
| 33 | + const data = this.signedKeyData; |
| 34 | + if (!data) return []; |
| 35 | + return [ |
| 36 | + { label: 'Signed key', value: data['signed_key'] }, |
| 37 | + { label: 'Lease ID', value: data['lease_id'] }, |
| 38 | + { label: 'Renewable', value: data['renewable'] }, |
| 39 | + { label: 'Lease duration', value: data['lease_duration'] }, |
| 40 | + { label: 'Serial number', value: data['serial_number'] }, |
| 41 | + ].filter((f) => f.value != null && f.value !== ''); |
| 42 | + } |
| 43 | + |
| 44 | + get breadcrumbs() { |
| 45 | + const { backendPath, roleName } = this.args; |
| 46 | + return [ |
| 47 | + { label: backendPath, route: 'vault.cluster.secrets.backend', model: backendPath }, |
| 48 | + { label: 'Sign', route: 'vault.cluster.secrets.backend', model: backendPath }, |
| 49 | + { label: roleName, route: 'vault.cluster.secrets.backend.show', model: roleName }, |
| 50 | + { label: 'Sign SSH Key' }, |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + sign = task( |
| 55 | + waitFor(async (evt: Event) => { |
| 56 | + evt.preventDefault(); |
| 57 | + this.errorMessage = null; |
| 58 | + |
| 59 | + const { isValid, state, invalidFormMessage, data } = this.signForm.toJSON(); |
| 60 | + this.modelValidations = isValid ? null : state; |
| 61 | + this.invalidFormAlert = isValid ? null : invalidFormMessage; |
| 62 | + if (!isValid) return; |
| 63 | + try { |
| 64 | + const result = await this.api.secrets.sshSignCertificate( |
| 65 | + this.args.roleName, |
| 66 | + this.args.backendPath, |
| 67 | + data |
| 68 | + ); |
| 69 | + this.signedKeyData = { ...result, ...(result.data as Record<string, unknown>) }; |
| 70 | + } catch (error) { |
| 71 | + const { message, response } = await this.api.parseError(error); |
| 72 | + if (response?.isControlGroupError) { |
| 73 | + this.controlGroup.saveTokenFromError(response); |
| 74 | + this.errorMessage = this.controlGroup.logFromError(response).content; |
| 75 | + } else { |
| 76 | + this.errorMessage = message; |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + ); |
| 81 | + |
| 82 | + @action |
| 83 | + reset() { |
| 84 | + this.signedKeyData = null; |
| 85 | + this.signForm = new SshSignForm({ cert_type: 'user' }); |
| 86 | + this.errorMessage = null; |
| 87 | + this.modelValidations = null; |
| 88 | + this.invalidFormAlert = null; |
| 89 | + } |
| 90 | +} |
0 commit comments