|
| 1 | +/** |
| 2 | + * Copyright (c) 2023 Gitpod GmbH. All rights reserved. |
| 3 | + * Licensed under the GNU Affero General Public License (AGPL). |
| 4 | + * See License.AGPL.txt in the project root for license information. |
| 5 | + */ |
| 6 | + |
| 7 | +import { BUILTIN_INSTLLATION_ADMIN_USER_ID, TypeORM } from "@gitpod/gitpod-db/lib"; |
| 8 | +import { Organization, SSHPublicKeyValue, User } from "@gitpod/gitpod-protocol"; |
| 9 | +import { Experiments } from "@gitpod/gitpod-protocol/lib/experiments/configcat-server"; |
| 10 | +import * as chai from "chai"; |
| 11 | +import { Container } from "inversify"; |
| 12 | +import "mocha"; |
| 13 | +import { createTestContainer } from "../test/service-testing-container-module"; |
| 14 | +import { resetDB } from "@gitpod/gitpod-db/lib/test/reset-db"; |
| 15 | +import { SSHKeyService } from "./sshkey-service"; |
| 16 | +import { OrganizationService } from "../orgs/organization-service"; |
| 17 | +import { UserService } from "./user-service"; |
| 18 | +import { expectError } from "../test/expect-utils"; |
| 19 | +import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error"; |
| 20 | + |
| 21 | +const expect = chai.expect; |
| 22 | + |
| 23 | +describe("SSHKeyService", async () => { |
| 24 | + let container: Container; |
| 25 | + let ss: SSHKeyService; |
| 26 | + |
| 27 | + let member: User; |
| 28 | + let stranger: User; |
| 29 | + let org: Organization; |
| 30 | + |
| 31 | + const testSSHkeys: SSHPublicKeyValue[] = [ |
| 32 | + { |
| 33 | + name: "foo", |
| 34 | + key: "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBN+Mh3U/3We4VYtV1QmWUFIzFLTUeegl1Ao5/QGtCRGAZn8bxX9KlCrrWISIjSYAwCajIEGSPEZwPNMBoK8XD8Q= test@gitpod", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "bar", |
| 38 | + key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIK0wmN/Cr3JXqmLW7u+g9pTh+wyqDHpSQEIQczXkVx9q bar@gitpod", |
| 39 | + }, |
| 40 | + ]; |
| 41 | + |
| 42 | + beforeEach(async () => { |
| 43 | + container = createTestContainer(); |
| 44 | + Experiments.configureTestingClient({ |
| 45 | + centralizedPermissions: true, |
| 46 | + }); |
| 47 | + |
| 48 | + const orgService = container.get<OrganizationService>(OrganizationService); |
| 49 | + org = await orgService.createOrganization(BUILTIN_INSTLLATION_ADMIN_USER_ID, "myOrg"); |
| 50 | + const invite = await orgService.getOrCreateInvite(BUILTIN_INSTLLATION_ADMIN_USER_ID, org.id); |
| 51 | + |
| 52 | + const userService = container.get<UserService>(UserService); |
| 53 | + member = await userService.createUser({ |
| 54 | + organizationId: org.id, |
| 55 | + identity: { |
| 56 | + authId: "foo", |
| 57 | + authName: "bar", |
| 58 | + authProviderId: "github", |
| 59 | + primaryEmail: "[email protected]", |
| 60 | + }, |
| 61 | + }); |
| 62 | + await orgService.joinOrganization(member.id, invite.id); |
| 63 | + stranger = await userService.createUser({ |
| 64 | + identity: { |
| 65 | + authId: "foo2", |
| 66 | + authName: "bar2", |
| 67 | + authProviderId: "github", |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + ss = container.get(SSHKeyService); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(async () => { |
| 75 | + // Clean-up database |
| 76 | + await resetDB(container.get(TypeORM)); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should add ssh key", async () => { |
| 80 | + const resp1 = await ss.hasSSHPublicKey(member.id, member.id); |
| 81 | + expect(resp1).to.be.false; |
| 82 | + |
| 83 | + await ss.addSSHPublicKey(member.id, member.id, testSSHkeys[0]); |
| 84 | + |
| 85 | + const resp2 = await ss.hasSSHPublicKey(member.id, member.id); |
| 86 | + expect(resp2).to.be.true; |
| 87 | + |
| 88 | + await expectError(ErrorCodes.NOT_FOUND, ss.hasSSHPublicKey(stranger.id, member.id)); |
| 89 | + await expectError(ErrorCodes.NOT_FOUND, ss.addSSHPublicKey(stranger.id, member.id, testSSHkeys[0])); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should list ssh keys", async () => { |
| 93 | + await ss.addSSHPublicKey(member.id, member.id, testSSHkeys[0]); |
| 94 | + await ss.addSSHPublicKey(member.id, member.id, testSSHkeys[1]); |
| 95 | + |
| 96 | + const keys = await ss.getSSHPublicKeys(member.id, member.id); |
| 97 | + expect(keys.length).to.equal(2); |
| 98 | + expect(testSSHkeys.some((k) => k.name === keys[0].name && k.key === keys[0].key)).to.be.true; |
| 99 | + expect(testSSHkeys.some((k) => k.name === keys[1].name && k.key === keys[1].key)).to.be.true; |
| 100 | + |
| 101 | + await expectError(ErrorCodes.NOT_FOUND, ss.getSSHPublicKeys(stranger.id, member.id)); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should delete ssh keys", async () => { |
| 105 | + await ss.addSSHPublicKey(member.id, member.id, testSSHkeys[0]); |
| 106 | + await ss.addSSHPublicKey(member.id, member.id, testSSHkeys[1]); |
| 107 | + |
| 108 | + const keys = await ss.getSSHPublicKeys(member.id, member.id); |
| 109 | + expect(keys.length).to.equal(2); |
| 110 | + |
| 111 | + await ss.deleteSSHPublicKey(member.id, member.id, keys[0].id); |
| 112 | + |
| 113 | + const keys2 = await ss.getSSHPublicKeys(member.id, member.id); |
| 114 | + expect(keys2.length).to.equal(1); |
| 115 | + |
| 116 | + await expectError(ErrorCodes.NOT_FOUND, ss.deleteSSHPublicKey(stranger.id, member.id, keys[0].id)); |
| 117 | + }); |
| 118 | +}); |
0 commit comments