@@ -10,6 +10,9 @@ import (
1010 "encoding/base64"
1111 "encoding/hex"
1212 "encoding/json"
13+ "fmt"
14+ "io"
15+ "net/http"
1316 "net/url"
1417 "time"
1518
@@ -55,15 +58,22 @@ func ExchangeSSHKey(apiEndpoint string, identityMrn string, resourceMrn string)
5558 }, nil
5659}
5760
58- func ExchangeExternalToken (apiEndpoint string , audience string , issuerUri string , jsonToken string ) (* ServiceAccountCredentials , error ) {
61+ func ExchangeExternalToken (apiEndpoint string , audience string , issuerURI string ) (* ServiceAccountCredentials , error ) {
62+ // TODO: This is just a testing function to fetch a GCP identity token
63+ // it should change to be a generic function.
64+ jsonToken , err := fetchGCPIdentityToken (audience )
65+ if err != nil {
66+ return nil , err
67+ }
68+
5969 stsClient , err := NewSecureTokenServiceClient (apiEndpoint , ranger .DefaultHttpClient ())
6070 if err != nil {
6171 return nil , err
6272 }
6373
6474 resp , err := stsClient .ExchangeExternalToken (context .Background (), & ExchangeExternalTokenRequest {
6575 Audience : audience ,
66- IssuerUri : issuerUri ,
76+ IssuerUri : issuerURI ,
6777 JwtToken : jsonToken ,
6878 })
6979 if err != nil {
@@ -85,6 +95,30 @@ func ExchangeExternalToken(apiEndpoint string, audience string, issuerUri string
8595 return & creds , nil
8696}
8797
98+ // TODO: This is just a testing function to fetch a GCP identity token
99+ // it should change to be a generic function that checks the provider and fetches the token accordingly.
100+ func fetchGCPIdentityToken (audience string ) (string , error ) {
101+ url := fmt .Sprintf ("http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=%s" , audience )
102+ req , err := http .NewRequest ("GET" , url , nil )
103+ if err != nil {
104+ return "" , err
105+ }
106+ req .Header .Add ("Metadata-Flavor" , "Google" )
107+
108+ client := & http.Client {}
109+ resp , err := client .Do (req )
110+ if err != nil {
111+ return "" , err
112+ }
113+ defer resp .Body .Close ()
114+
115+ tokenBytes , err := io .ReadAll (resp .Body )
116+ if err != nil {
117+ return "" , err
118+ }
119+ return string (tokenBytes ), nil
120+ }
121+
88122// signClaims implements claims signing with ssh.Signer
89123//
90124// To generate a new SSH key use:
0 commit comments