Skip to content

Commit 1c40ad0

Browse files
committed
Remove unused LSP methods and add encrypt/decrypt tests
1 parent 5817506 commit 1c40ad0

File tree

12 files changed

+83
-1082
lines changed

12 files changed

+83
-1082
lines changed

src/auth/AuthProtocol.ts

Lines changed: 5 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,15 @@
1-
import {
2-
MessageDirection,
3-
ProtocolRequestType,
4-
ProtocolNotificationType,
5-
ProtocolRequestType0,
6-
} from 'vscode-languageserver';
7-
import {
8-
UpdateCredentialsParams,
9-
UpdateCredentialsResult,
10-
ConnectionMetadata,
11-
ListProfilesParams,
12-
ListProfilesResult,
13-
UpdateProfileParams,
14-
UpdateProfileResult,
15-
GetSsoTokenParams,
16-
GetSsoTokenResult,
17-
InvalidateSsoTokenParams,
18-
InvalidateSsoTokenResult,
19-
SsoTokenChangedParams,
20-
} from './AwsLspAuthTypes';
1+
import { MessageDirection, NotificationType } from 'vscode-languageserver';
2+
import { RequestType } from 'vscode-languageserver-protocol';
3+
import { UpdateCredentialsParams, UpdateCredentialsResult } from './AwsLspAuthTypes';
214

22-
// AWS Credential Requests
235
export const IamCredentialsUpdateRequest = Object.freeze({
246
method: 'aws/credentials/iam/update' as const,
257
messageDirection: MessageDirection.clientToServer,
26-
type: new ProtocolRequestType<UpdateCredentialsParams, UpdateCredentialsResult, never, void, void>(
27-
'aws/credentials/iam/update',
28-
),
8+
type: new RequestType<UpdateCredentialsParams, UpdateCredentialsResult, void>('aws/credentials/iam/update'),
299
} as const);
3010

31-
export const BearerCredentialsUpdateRequest = Object.freeze({
32-
method: 'aws/credentials/token/update' as const,
33-
messageDirection: MessageDirection.clientToServer,
34-
type: new ProtocolRequestType<UpdateCredentialsParams, void, never, void, void>('aws/credentials/token/update'),
35-
} as const);
36-
37-
export const GetConnectionMetadataRequest = Object.freeze({
38-
method: 'aws/credentials/getConnectionMetadata' as const,
39-
messageDirection: MessageDirection.serverToClient,
40-
type: new ProtocolRequestType0<ConnectionMetadata | null, never, void, void>(
41-
'aws/credentials/getConnectionMetadata',
42-
),
43-
} as const);
44-
45-
// AWS Credential Notifications
4611
export const IamCredentialsDeleteNotification = Object.freeze({
4712
method: 'aws/credentials/iam/delete' as const,
4813
messageDirection: MessageDirection.clientToServer,
49-
type: new ProtocolNotificationType<void, void>('aws/credentials/iam/delete'),
50-
} as const);
51-
52-
export const BearerCredentialsDeleteNotification = Object.freeze({
53-
method: 'aws/credentials/token/delete' as const,
54-
messageDirection: MessageDirection.clientToServer,
55-
type: new ProtocolNotificationType<void, void>('aws/credentials/token/delete'),
56-
} as const);
57-
58-
// AWS Identity Requests
59-
export const ListProfilesRequest = Object.freeze({
60-
method: 'aws/identity/listProfiles' as const,
61-
messageDirection: MessageDirection.serverToClient,
62-
type: new ProtocolRequestType<ListProfilesParams, ListProfilesResult | null, never, void, void>(
63-
'aws/identity/listProfiles',
64-
),
65-
} as const);
66-
67-
export const UpdateProfileRequest = Object.freeze({
68-
method: 'aws/identity/updateProfile' as const,
69-
messageDirection: MessageDirection.serverToClient,
70-
type: new ProtocolRequestType<UpdateProfileParams, UpdateProfileResult | null, never, void, void>(
71-
'aws/identity/updateProfile',
72-
),
73-
} as const);
74-
75-
export const GetSsoTokenRequest = Object.freeze({
76-
method: 'aws/identity/getSsoToken' as const,
77-
messageDirection: MessageDirection.serverToClient,
78-
type: new ProtocolRequestType<GetSsoTokenParams, GetSsoTokenResult | null, never, void, void>(
79-
'aws/identity/getSsoToken',
80-
),
81-
} as const);
82-
83-
export const InvalidateSsoTokenRequest = Object.freeze({
84-
method: 'aws/identity/invalidateSsoToken' as const,
85-
messageDirection: MessageDirection.serverToClient,
86-
type: new ProtocolRequestType<InvalidateSsoTokenParams, InvalidateSsoTokenResult | null, never, void, void>(
87-
'aws/identity/invalidateSsoToken',
88-
),
89-
} as const);
90-
91-
// AWS Identity Notifications
92-
export const SsoTokenChangedNotification = Object.freeze({
93-
method: 'aws/identity/ssoTokenChanged' as const,
94-
messageDirection: MessageDirection.both,
95-
type: new ProtocolNotificationType<SsoTokenChangedParams, void>('aws/identity/ssoTokenChanged'),
14+
type: new NotificationType<void>('aws/credentials/iam/delete'),
9615
} as const);

src/auth/AwsCredentials.ts

Lines changed: 2 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,7 @@ import { LoggerFactory } from '../telemetry/LoggerFactory';
88
import { extractErrorMessage } from '../utils/Errors';
99
import { getRegion } from '../utils/Region';
1010
import { parseWithPrettyError } from '../utils/ZodErrorWrapper';
11-
import {
12-
parseListProfilesResult,
13-
parseUpdateCredentialsParams,
14-
parseSsoTokenChangedParams,
15-
parseInvalidateSsoTokenParams,
16-
parseGetSsoTokenParams,
17-
parseUpdateProfileParams,
18-
parseGetSsoTokenResult,
19-
} from './AwsCredentialsParser';
20-
import {
21-
SsoConnectionType,
22-
BearerCredentials,
23-
ConnectionMetadata,
24-
UpdateCredentialsParams,
25-
ListProfilesResult,
26-
UpdateProfileParams,
27-
UpdateProfileResult,
28-
GetSsoTokenParams,
29-
GetSsoTokenResult,
30-
InvalidateSsoTokenParams,
31-
InvalidateSsoTokenResult,
32-
SsoTokenChangedParams,
33-
IamCredentials,
34-
} from './AwsLspAuthTypes';
11+
import { UpdateCredentialsParams, IamCredentials } from './AwsLspAuthTypes';
3512

3613
const DecryptedCredentialsSchema = z.object({
3714
data: z.object({
@@ -47,8 +24,6 @@ export class AwsCredentials {
4724
private readonly logger = LoggerFactory.getLogger(AwsCredentials);
4825

4926
private iamCredentials?: IamCredentials;
50-
private bearerCredentials?: BearerCredentials;
51-
private connectionMetadata?: ConnectionMetadata;
5227
private readonly encryptionKey: Buffer;
5328

5429
constructor(
@@ -66,96 +41,9 @@ export class AwsCredentials {
6641
return structuredClone(this.iamCredentials);
6742
}
6843

69-
getBearer(): DeepReadonly<BearerCredentials> {
70-
if (!this.bearerCredentials) {
71-
throw new Error('Bearer credentials not configured');
72-
}
73-
return structuredClone(this.bearerCredentials);
74-
}
75-
76-
getConnectionMetadata(): ConnectionMetadata | undefined {
77-
return this.connectionMetadata;
78-
}
79-
80-
getConnectionType(): SsoConnectionType {
81-
const startUrl = this.connectionMetadata?.sso?.startUrl;
82-
if (!startUrl) return 'none';
83-
84-
return startUrl.includes('view.awsapps.com/start') ? 'builderId' : 'identityCenter';
85-
}
86-
87-
async listProfiles(): Promise<ListProfilesResult | undefined> {
88-
try {
89-
const result = await this.awsHandlers.sendListProfiles({});
90-
if (!result) return undefined;
91-
92-
const parsedResult = parseListProfilesResult(result);
93-
94-
this.logger.info(`Found ${parsedResult.profiles.length} profiles`);
95-
return parsedResult;
96-
} catch (error) {
97-
this.logger.error({ error }, 'Failed to list profiles');
98-
return undefined;
99-
}
100-
}
101-
102-
async updateProfile(params: UpdateProfileParams): Promise<UpdateProfileResult | undefined> {
103-
try {
104-
const parsedParams = parseUpdateProfileParams(params);
105-
const result = await this.awsHandlers.sendUpdateProfile(parsedParams);
106-
107-
this.logger.info(`Profile updated: ${parsedParams.profile.name}`);
108-
return result ?? undefined;
109-
} catch (error) {
110-
this.logger.error({ error }, 'Failed to update profile');
111-
return undefined;
112-
}
113-
}
114-
115-
async getSsoToken(params: GetSsoTokenParams): Promise<GetSsoTokenResult | undefined> {
116-
try {
117-
const parsedParams = parseGetSsoTokenParams(params);
118-
const result = await this.awsHandlers.sendGetSsoToken(parsedParams);
119-
120-
if (!result?.ssoToken) return result ?? undefined;
121-
122-
const parsedResult = parseGetSsoTokenResult(result);
123-
this.logger.info('Retrieved SSO token');
124-
125-
const { data, metadata } = parsedResult.updateCredentialsParams;
126-
if (data && 'token' in data) {
127-
this.bearerCredentials = data;
128-
if (metadata) {
129-
this.connectionMetadata = metadata;
130-
}
131-
}
132-
133-
return parsedResult;
134-
} catch (error) {
135-
this.logger.error({ error }, 'Failed to get SSO token');
136-
return undefined;
137-
}
138-
}
139-
140-
async invalidateSsoToken(params: InvalidateSsoTokenParams): Promise<InvalidateSsoTokenResult | undefined> {
141-
try {
142-
const parsedParams = parseInvalidateSsoTokenParams(params);
143-
const result = await this.awsHandlers.sendInvalidateSsoToken(parsedParams);
144-
145-
this.bearerCredentials = undefined;
146-
this.connectionMetadata = undefined;
147-
148-
this.logger.info('SSO token invalidated');
149-
return result ?? undefined;
150-
} catch (error) {
151-
this.logger.error({ error }, 'Failed to invalidate SSO token');
152-
return undefined;
153-
}
154-
}
155-
15644
async handleIamCredentialsUpdate(params: UpdateCredentialsParams): Promise<boolean> {
15745
try {
158-
const decrypted = await compactDecrypt(params.data as unknown as string, this.encryptionKey);
46+
const decrypted = await compactDecrypt(params.data, this.encryptionKey);
15947
const rawCredentials = JSON.parse(new TextDecoder().decode(decrypted.plaintext)) as unknown;
16048

16149
const validatedCredentials = parseWithPrettyError(
@@ -181,46 +69,8 @@ export class AwsCredentials {
18169
}
18270
}
18371

184-
handleBearerCredentialsUpdate(params: UpdateCredentialsParams) {
185-
try {
186-
const { data, metadata } = parseWithPrettyError(parseUpdateCredentialsParams, params);
187-
188-
if ('token' in data) {
189-
this.bearerCredentials = data;
190-
if (metadata) {
191-
this.connectionMetadata = metadata;
192-
}
193-
this.logger.info('Updated bearer credentials');
194-
}
195-
} catch (error) {
196-
this.logger.error(`Failed to update Bearer token: ${extractErrorMessage(error)}`);
197-
this.bearerCredentials = undefined;
198-
this.connectionMetadata = undefined;
199-
}
200-
}
201-
20272
handleIamCredentialsDelete() {
20373
this.logger.info('IAM credentials deleted');
20474
this.iamCredentials = undefined;
20575
}
206-
207-
handleBearerCredentialsDelete() {
208-
this.logger.info('Bearer credentials deleted');
209-
this.bearerCredentials = undefined;
210-
this.connectionMetadata = undefined;
211-
}
212-
213-
handleSsoTokenChanged(params: SsoTokenChangedParams) {
214-
try {
215-
const { kind } = parseSsoTokenChangedParams(params);
216-
if (kind === 'Expired') {
217-
this.bearerCredentials = undefined;
218-
this.connectionMetadata = undefined;
219-
} else if (kind === 'Refreshed') {
220-
this.logger.info('SSO token refreshed');
221-
}
222-
} catch (error) {
223-
this.logger.error({ error }, 'Error handling SSO token change');
224-
}
225-
}
22676
}

0 commit comments

Comments
 (0)