Skip to content

Commit a525a4e

Browse files
yesnaultrichardlt
authored andcommitted
feat(ui): auto create ssh/gpg key at project creation (#3802)
* feat(ui): auto create ssh/gpg key at project creation Signed-off-by: Yvonnick Esnault <[email protected]> * cr Signed-off-by: Yvonnick Esnault <[email protected]> * cr Signed-off-by: Yvonnick Esnault <[email protected]> * Update ui/src/assets/i18n/fr.json Co-Authored-By: yesnault <[email protected]>
1 parent 94e77c7 commit a525a4e

File tree

7 files changed

+35
-65
lines changed

7 files changed

+35
-65
lines changed

engine/api/project.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"context"
55
"database/sql"
6+
"fmt"
67
"net/http"
78
"regexp"
89
"strings"
@@ -451,6 +452,28 @@ func (api *API) addProjectHandler() service.Handler {
451452
}
452453
}
453454

455+
var sshExists, gpgExists bool
456+
for _, k := range p.Keys {
457+
switch k.Type {
458+
case sdk.KeyTypeSSH:
459+
sshExists = true
460+
case sdk.KeyTypePGP:
461+
gpgExists = true
462+
}
463+
}
464+
465+
if !sshExists {
466+
p.Keys = append(p.Keys, sdk.ProjectKey{Key: sdk.Key{
467+
Type: sdk.KeyTypeSSH,
468+
Name: fmt.Sprintf("proj-%s-%s", sdk.KeyTypeSSH, strings.ToLower(p.Key))},
469+
})
470+
}
471+
if !gpgExists {
472+
p.Keys = append(p.Keys, sdk.ProjectKey{Key: sdk.Key{
473+
Type: sdk.KeyTypePGP,
474+
Name: fmt.Sprintf("proj-%s-%s", sdk.KeyTypePGP, strings.ToLower(p.Key))},
475+
})
476+
}
454477
for _, k := range p.Keys {
455478
k.ProjectID = p.ID
456479
switch k.Type {

ui/src/app/shared/keys/form/keys.form.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<div class="KeyForm">
22
<div class="ui form">
3+
<div class="ui info message" *ngIf="newKey.type == 'ssh'">
4+
{{ 'keys_ssh_key_help' | translate }} <a href="https://ovh.github.io/cds/cli/worker/key/install/">Worker Install Key</a>
5+
</div>
6+
<div class="ui info message" *ngIf="newKey.type == 'pgp'">
7+
{{ 'keys_pgp_key_help' | translate }} <a href="https://ovh.github.io/cds/cli/worker/key/install/">Worker Install Key</a>
8+
</div>
39
<div class="fields">
410
<div class="seven wide field">
511
<label>{{ 'keys_name' | translate}}</label>
@@ -16,8 +22,7 @@
1622
[options]="keyTypes"
1723
[isSearchable]="true"
1824
#selectKeyType>
19-
<sui-select-option *ngFor="let t of selectKeyType.filteredOptions"
20-
[value]="t">
25+
<sui-select-option *ngFor="let t of selectKeyType.filteredOptions" [value]="t">
2126
</sui-select-option>
2227
</sui-select>
2328
</div>

ui/src/app/views/project/add/project.add.component.spec.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,14 @@ describe('CDS: Project Show Component', () => {
109109
expect(router.navigate).toHaveBeenCalled();
110110
});
111111

112-
it('it should generate an project key', () => {
113-
let fixture = TestBed.createComponent(ProjectAddComponent);
114-
fixture.componentInstance.generateKey('^r%t*$f#|m');
115-
expect(fixture.componentInstance.project.key).toBe('RTFM');
116-
117-
});
118-
119112
it('it should generate errors', () => {
120113
let fixture = TestBed.createComponent(ProjectAddComponent);
121-
fixture.componentInstance.addSshKey = true;
122114
fixture.componentInstance.createProject();
123115

124116
expect(fixture.componentInstance.nameError).toBeTruthy();
125-
expect(fixture.componentInstance.keyError).toBeTruthy();
126-
expect(fixture.componentInstance.sshError).toBeTruthy();
127117

128118
// pattern error
129-
fixture.componentInstance.project.key = 'aze';
130119
fixture.componentInstance.createProject();
131-
expect(fixture.componentInstance.keyError).toBeTruthy();
132120
});
133121
});
134122

ui/src/app/views/project/add/project.add.component.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import {Component, ViewChild} from '@angular/core';
22
import {Router} from '@angular/router';
33
import {TranslateService} from '@ngx-translate/core';
4-
import {cloneDeep} from 'lodash';
54
import {SemanticModalComponent} from 'ng-semantic/ng-semantic';
65
import {first} from 'rxjs/operators';
76
import {Group, GroupPermission} from '../../../model/group.model';
8-
import {Key, KeyType} from '../../../model/keys.model';
97
import {Project} from '../../../model/project.model';
108
import {GroupService} from '../../../service/group/group.service';
119
import {ProjectStore} from '../../../service/project/project.store';
@@ -22,13 +20,10 @@ export class ProjectAddComponent {
2220
project: Project;
2321
newGroup: Group = new Group();
2422
group: Group = new Group();
25-
addSshKey = false;
26-
sshKey: Key;
2723

2824
loading = false;
2925
nameError = false;
3026
keyError = false;
31-
sshError = false;
3227
fileTooLarge = false;
3328

3429
groupList: Group[];
@@ -39,8 +34,6 @@ export class ProjectAddComponent {
3934
constructor(private _projectStore: ProjectStore, private _toast: ToastService, private _translate: TranslateService,
4035
private _router: Router, private _groupService: GroupService, private _permissionService: PermissionService) {
4136
this.project = new Project();
42-
this.sshKey = new Key();
43-
this.sshKey.type = KeyType.SSH;
4437
this.loadGroups(null);
4538
}
4639

@@ -57,7 +50,6 @@ export class ProjectAddComponent {
5750
}
5851
this.project.key = name.toUpperCase();
5952
this.project.key = this.project.key.replace(/([.,; *`§%&#_\-'+?^=!:$\\"{}()|\[\]\/\\])/g, '').substr(0, 5);
60-
this.sshKey.name = 'sshkey';
6153
}
6254

6355
/**
@@ -67,7 +59,6 @@ export class ProjectAddComponent {
6759
this.loading = true;
6860
this.nameError = false;
6961
this.keyError = false;
70-
this.sshError = false;
7162
if (!this.project.name || this.project.name.length === 0) {
7263
this.nameError = true;
7364
}
@@ -88,21 +79,7 @@ export class ProjectAddComponent {
8879
this.project.groups.push(gp);
8980
}
9081

91-
if (this.addSshKey && (!this.sshKey.name || this.sshKey.name === '')) {
92-
this.sshError = true;
93-
}
94-
95-
if (!this.nameError && !this.keyError && !this.sshError) {
96-
if (this.addSshKey) {
97-
this.project.keys = new Array<Key>();
98-
99-
let sshKeyCloned = cloneDeep(this.sshKey);
100-
if (sshKeyCloned.name.indexOf('proj-') !== 0) {
101-
sshKeyCloned.name = 'proj-' + sshKeyCloned.name;
102-
}
103-
this.project.keys.push(sshKeyCloned);
104-
}
105-
82+
if (!this.nameError && !this.keyError) {
10683
this._projectStore.createProject(this.project).subscribe(p => {
10784
this.loading = true;
10885
this._toast.success('', this._translate.instant('project_added'));

ui/src/app/views/project/add/project.add.html

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,6 @@ <h2>{{ 'project_create' | translate }}</h2>
3636

3737
<hr>
3838

39-
<div class="ui message">
40-
{{'project_add_ssh_key_help' | translate}}
41-
</div>
42-
<div class="field">
43-
<div class="ui checkbox">
44-
<input type="checkbox" id="sshkey" name="sshkey" [(ngModel)]="addSshKey">
45-
<label for="sshkey">{{ 'project_add_ssh_key' | translate }}</label>
46-
</div>
47-
</div>
48-
<div class="field" *ngIf="addSshKey">
49-
<label>{{ 'wizard_ssh_key_name' | translate }}</label>
50-
<div class="ui labeled input">
51-
<div class="ui label">proj-</div>
52-
<input type="text" name="keyname" [(ngModel)]="sshKey.name">
53-
</div>
54-
55-
</div>
56-
<div class="ui error message" *ngIf="sshError">
57-
{{ 'project_ssh_error' | translate }}
58-
</div>
59-
60-
<hr>
61-
6239
<div class="ui message">
6340
{{'project_add_group_help' | translate}}
6441
</div>

ui/src/assets/i18n/en.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@
347347
"keys_removed": "Key has been removed",
348348
"keys_type": "Key type",
349349
"keys_no": "There is no keys",
350+
"keys_ssh_key_help": "A SSH Key is useful when you want to git clone some code from a Git Repository. CDS can generate a key pair and you can copy / paste the public key to your access keys on your repositories. If you want to use manually the key, please read:",
351+
"keys_pgp_key_help": "A PGP Key is useful when you want to git tag a repository. If you want to use manually the key, please read:",
350352

351353
"usage_application_list": "Applications used",
352354
"usage_workflow_list": "Workflows used",
@@ -631,8 +633,6 @@
631633
"project_list_card_updated": "Updated the {{date}}",
632634
"project_advanced_title": "Project administration",
633635
"project_added": "Project has just been created",
634-
"project_add_ssh_key": "Generate a ssh key",
635-
"project_add_ssh_key_help": "A SSK Key is useful when you want to git clone some code from a Git Repository. CDS can generate a key pair and you can copy / paste the public key to your access keys on your repositories.",
636636
"project_add_group_help": "Read / Write / Execute permissions have to be set on this new project. You can choose an existing users group or let empty to automatically create a new group, you will be administrator of this group.",
637637
"project_applications_list": "List of applications in the project: ",
638638
"project_create": "Create a new project",

ui/src/assets/i18n/fr.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@
347347
"keys_removed": "La clé a été supprimée",
348348
"keys_type": "Type de clé",
349349
"keys_no": "Il n'y a aucune clé",
350+
"keys_ssh_key_help": "Une clé SSH est généralement utile lorsque vous souhaitez cloner un dépôt Git. CDS peut générer une paire de clé, il ne vous restera qu'à copier / coller la clé publique fournie par CDS dans les clés autorisées sur vos dépôts. Si vous souhaitez utiliser cette clé manuellement, consultez la documentation:",
351+
"keys_pgp_key_help": "Une clé PGP est généralement utile lorsque vous souhaitez tagguer un dépôt Git. Si vous souhaitez utiliser cette clé manuellement, consultez la documentation:",
350352

351353
"usage_application_list": "Applications utilisées",
352354
"usage_workflow_list": "Workflows utilisés",
@@ -631,8 +633,6 @@
631633
"project_list_card_updated": "Mis à jour le {{date}}",
632634
"project_advanced_title": "Administration du projet",
633635
"project_added": "Projet créé",
634-
"project_add_ssh_key": "Générer une clé ssh",
635-
"project_add_ssh_key_help": "Une clé ssh est généralement utile lorsque vous souhaitez cloner un dépôt Git. CDS peut générer une paire de clé, il ne vous restera qu'à copier / coller la clé publique fournie par CDS dans les clés autorisées sur vos dépôts.",
636636
"project_add_group_help": "Les permissions de Lecture / Écriture / Exécution doivent être positionnées sur ce nouveau projet. Vous pouvez choisir un groupe d'utilisateurs existant ou laisser vide pour que CDS créé automatiquement un nouveau groupe, vous en serez l'administrateur.",
637637
"project_applications_list": "Liste des applications du projet : ",
638638
"project_create": "Création d'un projet",

0 commit comments

Comments
 (0)