Skip to content

Commit efc49ae

Browse files
committed
imp(sdk): keep legacy support only in TappdClient.
1 parent 13d2594 commit efc49ae

File tree

2 files changed

+113
-28
lines changed

2 files changed

+113
-28
lines changed

sdk/js/src/__tests__/index.test.ts

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { expect, describe, it, vi } from 'vitest'
2-
import { DstackClient } from '../index'
2+
import { DstackClient, TappdClient } from '../index'
33

44
describe('DstackClient', () => {
5-
it('should able to derive key', async () => {
6-
const client = new DstackClient()
5+
it('should able to derive key in TappdClient', async () => {
6+
const client = new TappdClient()
77
const result = await client.deriveKey('/', 'test')
88
expect(result).toHaveProperty('key')
99
expect(result).toHaveProperty('certificate_chain')
1010
})
1111

12+
it('should throws error in DstackClient', async () => {
13+
const client = new DstackClient()
14+
await expect(() => client.deriveKey('/', 'test')).rejects.toThrow('deriveKey is deprecated, please use getKey instead.')
15+
})
16+
1217
it('should able to get key', async () => {
1318
const client = new DstackClient()
1419
const result = await client.getKey('/', 'test')
@@ -141,9 +146,9 @@ describe('DstackClient', () => {
141146
expect(typeof isReachable).toBe('boolean')
142147
})
143148

144-
describe('deprecated methods', () => {
149+
describe('deprecated methods with TappdClient', () => {
145150
it('should support deprecated deriveKey method with warning', async () => {
146-
const client = new DstackClient()
151+
const client = new TappdClient()
147152
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
148153

149154
const result = await client.deriveKey('/', 'test')
@@ -155,7 +160,7 @@ describe('DstackClient', () => {
155160
})
156161

157162
it('should support deprecated tdxQuote method with warning', async () => {
158-
const client = new DstackClient()
163+
const client = new TappdClient()
159164
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
160165

161166
const result = await client.tdxQuote('test data')
@@ -167,7 +172,7 @@ describe('DstackClient', () => {
167172
})
168173

169174
it('should support tdxQuote with hash algorithm parameter', async () => {
170-
const client = new DstackClient()
175+
const client = new TappdClient()
171176
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
172177

173178
const result = await client.tdxQuote('test data', 'sha256')
@@ -178,4 +183,54 @@ describe('DstackClient', () => {
178183
consoleSpy.mockRestore()
179184
})
180185
})
186+
187+
describe('deprecated methods with DstackClient', () => {
188+
it('should throws error in deriveKey method', async () => {
189+
const client = new DstackClient()
190+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
191+
192+
await expect(() => client.deriveKey('/', 'test')).rejects.toThrow('deriveKey is deprecated, please use getKey instead.')
193+
194+
consoleSpy.mockRestore()
195+
})
196+
197+
it('should throws error in tdxQuote method without hash algorithm parameter', async () => {
198+
const client = new DstackClient()
199+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
200+
201+
await expect(() => client.tdxQuote('test data')).rejects.toThrow('tdxQuote only supports raw hash algorithm.')
202+
203+
consoleSpy.mockRestore()
204+
})
205+
206+
it("should throws error in tdxQuote method with hash algorithm parameter other than raw", async () => {
207+
const client = new DstackClient()
208+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
209+
210+
await expect(() => client.tdxQuote('test data', 'sha256')).rejects.toThrow('tdxQuote only supports raw hash algorithm.')
211+
212+
consoleSpy.mockRestore()
213+
})
214+
215+
it('should able to get quote with plain report_data in tdxQuote method with warning', async () => {
216+
const client = new DstackClient()
217+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
218+
219+
const result = await client.tdxQuote('test data', "raw")
220+
expect(result).toHaveProperty('quote')
221+
expect(result).toHaveProperty('event_log')
222+
expect(consoleSpy).toHaveBeenCalledWith('tdxQuote is deprecated, please use getQuote instead')
223+
224+
consoleSpy.mockRestore()
225+
})
226+
227+
it('should throws error in tdxQuote with hash algorithm parameter', async () => {
228+
const client = new DstackClient()
229+
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
230+
231+
await expect(() => client.tdxQuote('test data', 'sha256')).rejects.toThrow('tdxQuote only supports raw hash algorithm.')
232+
233+
consoleSpy.mockRestore()
234+
})
235+
})
181236
})

sdk/js/src/index.ts

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,21 @@ export interface TlsKeyOptions {
125125
}
126126

127127
export class DstackClient {
128-
private endpoint: string
129-
private tappdEndpoint: string
128+
protected endpoint: string
130129

131-
constructor(endpoint: string = '/var/run/dstack.sock') {
132-
var tappdEndpoint = '/var/run/tappd.sock'
133-
if (process.env.DSTACK_SIMULATOR_ENDPOINT) {
134-
console.warn(`Using simulator endpoint: ${process.env.DSTACK_SIMULATOR_ENDPOINT}`)
135-
endpoint = process.env.DSTACK_SIMULATOR_ENDPOINT
136-
}
137-
if (process.env.TAPPD_SIMULATOR_ENDPOINT) {
138-
console.warn(`Using tappd endpoint: ${process.env.TAPPD_SIMULATOR_ENDPOINT}`)
139-
tappdEndpoint = process.env.TAPPD_SIMULATOR_ENDPOINT
130+
constructor(endpoint: string | undefined = undefined) {
131+
if (endpoint === undefined) {
132+
if (process.env.DSTACK_SIMULATOR_ENDPOINT) {
133+
console.warn(`Using simulator endpoint: ${process.env.DSTACK_SIMULATOR_ENDPOINT}`)
134+
endpoint = process.env.DSTACK_SIMULATOR_ENDPOINT
135+
} else {
136+
endpoint = '/var/run/dstack.sock'
137+
}
140138
}
141139
if (endpoint.startsWith('/') && !fs.existsSync(endpoint)) {
142140
throw new Error(`Unix socket file ${endpoint} does not exist`);
143141
}
144142
this.endpoint = endpoint
145-
this.tappdEndpoint = tappdEndpoint
146143
}
147144

148145
async getKey(path: string, purpose: string = ''): Promise<GetKeyResponse> {
@@ -251,6 +248,46 @@ export class DstackClient {
251248
// These methods don't mean fully compatible as past, but we keep them here until next major version.
252249
//
253250

251+
/**
252+
* @deprecated Use getKey instead.
253+
* @param path The path to the key.
254+
* @param subject The subject of the key.
255+
* @param altNames The alternative names of the key.
256+
* @returns The key.
257+
*/
258+
async deriveKey(path?: string, subject?: string, altNames?: string[]): Promise<GetTlsKeyResponse> {
259+
throw new Error('deriveKey is deprecated, please use getKey instead.')
260+
}
261+
262+
/**
263+
* @deprecated Use getQuote instead.
264+
* @param report_data The report data.
265+
* @param hash_algorithm The hash algorithm.
266+
* @returns The quote.
267+
*/
268+
async tdxQuote(report_data: string | Buffer | Uint8Array, hash_algorithm?: TdxQuoteHashAlgorithms): Promise<GetQuoteResponse> {
269+
console.warn('tdxQuote is deprecated, please use getQuote instead')
270+
if (hash_algorithm !== "raw") {
271+
throw new Error('tdxQuote only supports raw hash algorithm.')
272+
}
273+
return this.getQuote(report_data)
274+
}
275+
}
276+
277+
export class TappdClient extends DstackClient {
278+
constructor(endpoint: string | undefined = undefined) {
279+
if (endpoint === undefined) {
280+
if (process.env.TAPPD_SIMULATOR_ENDPOINT) {
281+
console.warn(`Using tappd endpoint: ${process.env.TAPPD_SIMULATOR_ENDPOINT}`)
282+
endpoint = process.env.TAPPD_SIMULATOR_ENDPOINT
283+
} else {
284+
endpoint = '/var/run/tappd.sock'
285+
}
286+
}
287+
console.warn('TappdClient is deprecated, please use DstackClient instead')
288+
super(endpoint)
289+
}
290+
254291
/**
255292
* @deprecated Use getKey instead.
256293
* @param path The path to the key.
@@ -265,7 +302,7 @@ export class DstackClient {
265302
raw['alt_names'] = alt_names
266303
}
267304
const payload = JSON.stringify(raw)
268-
const result = await send_rpc_request<GetTlsKeyResponse>(this.tappdEndpoint, '/prpc/Tappd.DeriveKey', payload)
305+
const result = await send_rpc_request<GetTlsKeyResponse>(this.endpoint, '/prpc/Tappd.DeriveKey', payload)
269306
Object.defineProperty(result, 'asUint8Array', {
270307
get: () => (length?: number) => x509key_to_uint8array(result.key, length),
271308
enumerable: true,
@@ -292,7 +329,7 @@ export class DstackClient {
292329
}
293330
}
294331
const payload = JSON.stringify({ report_data: hex, hash_algorithm })
295-
const result = await send_rpc_request<GetQuoteResponse>(this.tappdEndpoint, '/prpc/Tappd.TdxQuote', payload)
332+
const result = await send_rpc_request<GetQuoteResponse>(this.endpoint, '/prpc/Tappd.TdxQuote', payload)
296333
if ('error' in result) {
297334
const err = result['error'] as string
298335
throw new Error(err)
@@ -304,11 +341,4 @@ export class DstackClient {
304341
})
305342
return Object.freeze(result)
306343
}
307-
}
308-
309-
export class TappdClient extends DstackClient {
310-
constructor(endpoint: string = '/var/run/tappd.sock') {
311-
console.warn('TappdClient is deprecated, please use DstackClient instead')
312-
super(endpoint)
313-
}
314344
}

0 commit comments

Comments
 (0)