@@ -30,6 +30,51 @@ export async function initVaultClient() {
3030 return vc ;
3131}
3232
33+ export async function rotateVaultAccountAndAccessToken ( props : {
34+ project : Project ;
35+ projectSecretKey ?: string ;
36+ projectSecretHash ?: string ;
37+ } ) {
38+ const vaultClient = await initVaultClient ( ) ;
39+ const service = props . project . services . find (
40+ ( service ) => service . name === "engineCloud" ,
41+ ) ;
42+ const storedRotationCode = service ?. rotationCode ;
43+ if ( ! storedRotationCode ) {
44+ throw new Error ( "No rotation code found" ) ;
45+ }
46+
47+ const rotateServiceAccountRes = await rotateServiceAccount ( {
48+ client : vaultClient ,
49+ request : {
50+ auth : {
51+ rotationCode : storedRotationCode ,
52+ } ,
53+ } ,
54+ } ) ;
55+ if ( rotateServiceAccountRes . error ) {
56+ throw new Error ( rotateServiceAccountRes . error . message ) ;
57+ }
58+ const adminKey = rotateServiceAccountRes . data . newAdminKey ;
59+ const rotationCode = rotateServiceAccountRes . data . newRotationCode ;
60+
61+ const { managementToken, walletToken } =
62+ await createAndEncryptVaultAccessTokens ( {
63+ project : props . project ,
64+ projectSecretKey : props . projectSecretKey ,
65+ projectSecretHash : props . projectSecretHash ,
66+ vaultClient,
67+ adminKey,
68+ rotationCode,
69+ } ) ;
70+
71+ return {
72+ adminKey,
73+ managementToken,
74+ walletToken,
75+ } ;
76+ }
77+
3378export async function createVaultAccountAndAccessToken ( props : {
3479 project : Project ;
3580 projectSecretKey ?: string ;
@@ -38,53 +83,26 @@ export async function createVaultAccountAndAccessToken(props: {
3883 try {
3984 const vaultClient = await initVaultClient ( ) ;
4085
41- const service = props . project . services . find (
42- ( service ) => service . name === "engineCloud" ,
43- ) ;
44- const storedRotationCode = service ?. rotationCode ;
45- const storedEncryptedAdminKey = service ?. encryptedAdminKey ;
46-
47- let adminKey : string | null = null ;
48- let rotationCode : string | null = null ;
49-
50- if ( storedRotationCode && storedEncryptedAdminKey ) {
51- // if the project has a managed vault admin key, rotate it
52- const rotateServiceAccountRes = await rotateServiceAccount ( {
53- client : vaultClient ,
54- request : {
55- auth : {
56- rotationCode : storedRotationCode ,
86+ const serviceAccountResult = await createServiceAccount ( {
87+ client : vaultClient ,
88+ request : {
89+ options : {
90+ metadata : {
91+ projectId : props . project . id ,
92+ purpose : "Thirdweb Project Server Wallet Service Account" ,
93+ teamId : props . project . teamId ,
5794 } ,
5895 } ,
59- } ) ;
60- if ( rotateServiceAccountRes . error ) {
61- throw new Error ( rotateServiceAccountRes . error . message ) ;
62- }
63- adminKey = rotateServiceAccountRes . data . newAdminKey ;
64- rotationCode = rotateServiceAccountRes . data . newRotationCode ;
65- } else {
66- // otherwise create a new service account
67- const serviceAccountResult = await createServiceAccount ( {
68- client : vaultClient ,
69- request : {
70- options : {
71- metadata : {
72- projectId : props . project . id ,
73- purpose : "Thirdweb Project Server Wallet Service Account" ,
74- teamId : props . project . teamId ,
75- } ,
76- } ,
77- } ,
78- } ) ;
79- if ( serviceAccountResult . success === false ) {
80- throw new Error (
81- `Failed to create service account: ${ serviceAccountResult . error } ` ,
82- ) ;
83- }
84- const serviceAccount = serviceAccountResult . data ;
85- adminKey = serviceAccount . adminKey ;
86- rotationCode = serviceAccount . rotationCode ;
96+ } ,
97+ } ) ;
98+ if ( serviceAccountResult . success === false ) {
99+ throw new Error (
100+ `Failed to create service account: ${ serviceAccountResult . error } ` ,
101+ ) ;
87102 }
103+ const serviceAccount = serviceAccountResult . data ;
104+ const adminKey = serviceAccount . adminKey ;
105+ const rotationCode = serviceAccount . rotationCode ;
88106
89107 const { managementToken, walletToken } =
90108 await createAndEncryptVaultAccessTokens ( {
0 commit comments