@@ -4,6 +4,7 @@ import { TerraformError } from '../interfaces/errors';
44import { Identity } from '../models/interfaces' ;
55import crypto from 'crypto' ;
66import { IdentityModel } from '../models/IdentityModel' ;
7+ import seedrandom from 'seedrandom' ;
78
89export type IdentityWithToken = Identity & {
910 token : string ;
@@ -44,6 +45,12 @@ export class GithubService {
4445 const decoded = Buffer . from ( token , 'base64' ) . toString ( 'utf8' ) ;
4546
4647 const [ username , password ] = decoded . split ( ':' ) ;
48+
49+ // Unauthenticated state storage for localstack
50+ if ( username === 'localstack' && password === 'localstack' ) {
51+ return this . inferLocalstackIdentity ( ) ;
52+ }
53+
4754 let owner : string | undefined ;
4855 let repo : string | undefined ;
4956 let workspace : string | undefined ;
@@ -205,4 +212,33 @@ export class GithubService {
205212 `Unable to determine owner and/or repository from token privileges. Ensure \`username\` is in the format of \`{owner}/{repository}\`, and the provided \`password\` (a GitHub token) has access to that repository.` ,
206213 ) ;
207214 } ;
215+
216+ // TODO: support 'who' from State Lock Request
217+ private inferLocalstackIdentity = ( who = 'unknown@unknown' ) : IdentityWithToken => {
218+ const tokenSha = crypto . createHash ( 'sha256' ) . update ( who ) . digest ( ) . toString ( 'base64' ) ;
219+
220+ const [ username , host ] = who . split ( '@' ) ;
221+ if ( ! username || ! host ) {
222+ throw new Error ( `Invalid format for \`Who\` on state lock request` ) ;
223+ }
224+
225+ // Set IDs as negative so they're clearly out of valid range
226+ const ownerId = seedrandom ( host ) . int32 ( ) * - 1 ;
227+ const repoId = seedrandom ( username ) . int32 ( ) * - 1 ;
228+
229+ return {
230+ pk : IdentityModel . prefix ( 'pk' , tokenSha ) ,
231+ sk : IdentityModel . prefix ( 'sk' ) ,
232+ owner : host ,
233+ ownerId,
234+ repo : username ,
235+ repoId,
236+ token : who ,
237+ tokenSha : tokenSha ,
238+ workspace : 'default' ,
239+ meta : {
240+ name : 'localstack' ,
241+ } ,
242+ } ;
243+ } ;
208244}
0 commit comments