@@ -140,16 +140,24 @@ function providerEnvironmentSecretName(input: {
140140}
141141
142142/**
143- * On disk the hub key is replaced by this marker and the real value lives in
144- * the secret store, mirroring provider environment secrets. A client that
145- * sends the marker back means "keep what you have".
143+ * On disk a hub key or Bitbucket token is replaced by this marker and the
144+ * real value lives in the secret store, mirroring provider environment
145+ * secrets. A client that sends the marker back means "keep what you have".
146146 */
147- const USAGE_LIMIT_SOURCE_KEY_REDACTED = "\u2022\u2022\u2022\u2022\u2022\u2022" ;
147+ const SECRET_REDACTED = "\u2022\u2022\u2022\u2022\u2022\u2022" ;
148148
149149function usageLimitSourceSecretName ( sourceId : string ) : string {
150150 return `usage-limit-source-${ Buffer . from ( sourceId , "utf8" ) . toString ( "base64url" ) } ` ;
151151}
152152
153+ const BITBUCKET_SECRET_NAMES = {
154+ accessToken : "bitbucket-access-token" ,
155+ apiToken : "bitbucket-api-token" ,
156+ } as const ;
157+ const BITBUCKET_SECRET_FIELDS = [ "accessToken" , "apiToken" ] as const ;
158+
159+ const redactSecret = ( value : string ) => ( value . length > 0 ? SECRET_REDACTED : "" ) ;
160+
153161function redactProviderEnvironmentVariable (
154162 variable : ProviderInstanceEnvironmentVariable ,
155163) : ProviderInstanceEnvironmentVariable {
@@ -182,11 +190,16 @@ export function redactServerSettingsForClient(settings: ServerSettings): ServerS
182190 id ,
183191 {
184192 ...source ,
185- managementKey : source . managementKey . length > 0 ? USAGE_LIMIT_SOURCE_KEY_REDACTED : "" ,
193+ managementKey : redactSecret ( source . managementKey ) ,
186194 } ,
187195 ] ) ,
188196 ) ;
189- return { ...settings , providerInstances, usageLimitSources } ;
197+ const bitbucket = {
198+ ...settings . bitbucket ,
199+ accessToken : redactSecret ( settings . bitbucket . accessToken ) ,
200+ apiToken : redactSecret ( settings . bitbucket . apiToken ) ,
201+ } ;
202+ return { ...settings , providerInstances, usageLimitSources, bitbucket } ;
190203}
191204
192205export class ServerSettingsService extends Context . Service <
@@ -555,6 +568,35 @@ const make = Effect.gen(function* () {
555568 ) ,
556569 ) ;
557570
571+ /**
572+ * Moves Bitbucket tokens hand-edited into settings.json into the secret store as they load,
573+ * so plaintext does not stay on disk. If the store is unavailable, the token keeps working
574+ * from the file and the move is retried on the next load.
575+ */
576+ const moveInlineBitbucketTokens = ( settings : ServerSettings ) =>
577+ Effect . gen ( function * ( ) {
578+ const bitbucket = { ...settings . bitbucket } ;
579+ let moved = false ;
580+ for ( const field of BITBUCKET_SECRET_FIELDS ) {
581+ const value = bitbucket [ field ] ;
582+ if ( value . length === 0 || value === SECRET_REDACTED ) continue ;
583+ const stored = yield * secretStore
584+ . set ( BITBUCKET_SECRET_NAMES [ field ] , textEncoder . encode ( value ) )
585+ . pipe (
586+ Effect . as ( true ) ,
587+ Effect . catch ( ( ) =>
588+ Effect . logWarning ( "failed to move a Bitbucket token into the secret store" , {
589+ field,
590+ } ) . pipe ( Effect . as ( false ) ) ,
591+ ) ,
592+ ) ;
593+ if ( ! stored ) continue ;
594+ bitbucket [ field ] = SECRET_REDACTED ;
595+ moved = true ;
596+ }
597+ return moved ? { ...settings , bitbucket } : settings ;
598+ } ) ;
599+
558600 const loadSettingsFromDisk = Effect . gen ( function * ( ) {
559601 let settings = DEFAULT_SERVER_SETTINGS ;
560602 let persisted : typeof PersistedOptionalProviderSettings . Type = { } ;
@@ -639,10 +681,12 @@ const make = Effect.gen(function* () {
639681 const folded = settingsFileTrusted
640682 ? foldLegacyProjectSettings ( loaded , legacyProjectRows )
641683 : loaded ;
642- if ( folded !== loaded ) {
643- yield * writeSettingsAtomically ( folded ) ;
684+ // Only rewrite a file that decoded cleanly; an untrusted one stays for the user to repair.
685+ const migrated = settingsFileTrusted ? yield * moveInlineBitbucketTokens ( folded ) : folded ;
686+ if ( migrated !== loaded ) {
687+ yield * writeSettingsAtomically ( migrated ) ;
644688 }
645- return folded ;
689+ return migrated ;
646690 } ) ;
647691
648692 const settingsCache = yield * Cache . make < typeof cacheKey , ServerSettings , ServerSettingsError > ( {
@@ -693,7 +737,7 @@ const make = Effect.gen(function* () {
693737 }
694738 const usageLimitSources : Record < string , UsageLimitSourceConfig > = { } ;
695739 for ( const [ sourceId , source ] of Object . entries ( settings . usageLimitSources ) ) {
696- if ( source . managementKey !== USAGE_LIMIT_SOURCE_KEY_REDACTED ) {
740+ if ( source . managementKey !== SECRET_REDACTED ) {
697741 usageLimitSources [ sourceId ] = source ;
698742 continue ;
699743 }
@@ -709,10 +753,23 @@ const make = Effect.gen(function* () {
709753 managementKey : Option . isSome ( secret ) ? textDecoder . decode ( secret . value ) : "" ,
710754 } ;
711755 }
756+ const bitbucket = { ...settings . bitbucket } ;
757+ for ( const field of BITBUCKET_SECRET_FIELDS ) {
758+ if ( bitbucket [ field ] !== SECRET_REDACTED ) continue ;
759+ const secret = yield * secretStore
760+ . get ( BITBUCKET_SECRET_NAMES [ field ] )
761+ . pipe (
762+ Effect . mapError (
763+ ( cause ) => new ServerSettingsError ( { settingsPath, operation : "read-secret" , cause } ) ,
764+ ) ,
765+ ) ;
766+ bitbucket [ field ] = Option . isSome ( secret ) ? textDecoder . decode ( secret . value ) : "" ;
767+ }
712768 return {
713769 ...settings ,
714770 providerInstances : providerInstances as ServerSettings [ "providerInstances" ] ,
715771 usageLimitSources : usageLimitSources as ServerSettings [ "usageLimitSources" ] ,
772+ bitbucket,
716773 } ;
717774 } ) ;
718775
@@ -829,7 +886,7 @@ const make = Effect.gen(function* () {
829886 const usageLimitSources : Record < string , UsageLimitSourceConfig > = { } ;
830887 for ( const [ sourceId , source ] of Object . entries ( next . usageLimitSources ) ) {
831888 const secretName = usageLimitSourceSecretName ( sourceId ) ;
832- if ( source . managementKey === USAGE_LIMIT_SOURCE_KEY_REDACTED ) {
889+ if ( source . managementKey === SECRET_REDACTED ) {
833890 usageLimitSources [ sourceId ] = source ;
834891 continue ;
835892 }
@@ -843,7 +900,7 @@ const make = Effect.gen(function* () {
843900 secretName,
844901 value : textEncoder . encode ( source . managementKey ) ,
845902 } ) ;
846- usageLimitSources [ sourceId ] = { ...source , managementKey : USAGE_LIMIT_SOURCE_KEY_REDACTED } ;
903+ usageLimitSources [ sourceId ] = { ...source , managementKey : SECRET_REDACTED } ;
847904 }
848905 for ( const sourceId of Object . keys ( current . usageLimitSources ) ) {
849906 if ( sourceId in next . usageLimitSources ) continue ;
@@ -854,11 +911,31 @@ const make = Effect.gen(function* () {
854911 } ) ;
855912 }
856913
914+ const bitbucket = { ...next . bitbucket } ;
915+ for ( const field of BITBUCKET_SECRET_FIELDS ) {
916+ let value = bitbucket [ field ] ;
917+ if ( value === SECRET_REDACTED ) {
918+ // The marker keeps what is saved. A plaintext value hand-edited into settings.json
919+ // is not in the secret store yet, so move it there instead of dropping it.
920+ const inline = current . bitbucket [ field ] ;
921+ if ( inline === SECRET_REDACTED || inline . length === 0 ) continue ;
922+ value = inline ;
923+ }
924+ const secretName = BITBUCKET_SECRET_NAMES [ field ] ;
925+ if ( value . length === 0 ) {
926+ changes . push ( { kind : "remove" , secretName, operation : "remove-secret" } ) ;
927+ continue ;
928+ }
929+ changes . push ( { kind : "write" , secretName, value : textEncoder . encode ( value ) } ) ;
930+ bitbucket [ field ] = SECRET_REDACTED ;
931+ }
932+
857933 return {
858934 settings : {
859935 ...next ,
860936 providerInstances : providerInstances as ServerSettings [ "providerInstances" ] ,
861937 usageLimitSources : usageLimitSources as ServerSettings [ "usageLimitSources" ] ,
938+ bitbucket,
862939 } ,
863940 changes,
864941 } ;
0 commit comments