@@ -31,11 +31,9 @@ import (
3131 "github.com/common-fate/granted/pkg/hook/accessrequesthook"
3232 "github.com/common-fate/sdk/config"
3333 accessv1alpha1 "github.com/common-fate/sdk/gen/commonfate/access/v1alpha1"
34- entityv1alpha1 "github.com/common-fate/sdk/gen/commonfate/entity/v1alpha1"
3534 "github.com/common-fate/sdk/handshake"
3635 "github.com/common-fate/sdk/service/access"
3736 "github.com/common-fate/sdk/service/access/grants"
38- "github.com/common-fate/sdk/service/entity"
3937 "github.com/common-fate/xid"
4038 "github.com/fatih/color"
4139 "github.com/hashicorp/yamux"
@@ -206,8 +204,6 @@ var proxyCommand = cli.Command{
206204
207205 }
208206
209- clio .Info ("Grant is activated" )
210-
211207 if result == nil || len (result .Grants ) == 0 {
212208 return errors .New ("could not load grant from Common Fate" )
213209 }
@@ -216,20 +212,15 @@ var proxyCommand = cli.Command{
216212
217213 grantsClient := grants .NewFromConfig (cfg )
218214
219- children , err := grab .AllPages (ctx , func (ctx context.Context , nextToken * string ) ([]* entityv1alpha1.Entity , * string , error ) {
220- res , err := grantsClient .QueryGrantChildren (ctx , connect .NewRequest (& accessv1alpha1.QueryGrantChildrenRequest {
221- Id : grant .Grant .Id ,
222- PageToken : grab .Value (nextToken ),
223- }))
224- if err != nil {
225- return nil , nil , err
226- }
227- return res .Msg .Entities , & res .Msg .NextPageToken , nil
228- })
215+ grantOutput , err := grantsClient .GetGrantOutput (ctx , connect .NewRequest (& accessv1alpha1.GetGrantOutputRequest {
216+ Id : grant .Grant .Id ,
217+ }))
229218 if err != nil {
230219 return err
231220 }
232221
222+ clio .Debugw ("found grant output" , "output" , grantOutput )
223+
233224 // find an unused local port to use for the ssm server
234225 // the user doesn't directly connect to this, they connect through our local proxy
235226 // which adds authentication
@@ -240,40 +231,33 @@ var proxyCommand = cli.Command{
240231
241232 clio .Debugf ("starting SSM portforward on local port: %s" , ssmPortforwardLocalPort )
242233
234+ rdsOutput , ok := grantOutput .Msg .Output .(* accessv1alpha1.GetGrantOutputResponse_OutputAwsRds )
235+ if ! ok {
236+ return errors .New ("unexpected grant output, this indicates an error in the Common Fate Provisioning process, you should contect your Common Fate administrator" )
237+ }
238+
243239 commandData := CommandData {
244- // the proxy server always runs on port 7070
240+ // the proxy server always runs on port 8080
245241 SSMPortForwardServerPort : "8080" ,
246242 SSMPortForwardLocalPort : ssmPortforwardLocalPort ,
243+ GrantOutput : rdsOutput .OutputAwsRds ,
247244 }
248245
249246 // in local dev we run on a different port because the control plane already runs on 8080
250247 if os .Getenv ("CF_DEV_PROXY" ) == "true" {
251248 commandData .SSMPortForwardServerPort = "7070"
252249 }
253250
254- for _ , child := range children {
255- if child .Eid .Type == GrantOutputType {
256- err = entity .Unmarshal (child , & commandData .GrantOutput )
257- if err != nil {
258- return err
259- }
260- }
261- }
262-
263- if commandData .GrantOutput .Grant .ID == "" {
264- return errors .New ("did not find a grant output entity in query grant children response" )
265- }
266-
267251 clio .Debugw ("command data" , "commandData" , commandData )
268252
269253 p := & cfaws.Profile {
270254 Name : grant .Grant .Id ,
271255 ProfileType : "AWS_SSO" ,
272256 AWSConfig : awsConfig.SharedConfig {
273- SSOAccountID : commandData .GrantOutput .Database . Account . ID ,
257+ SSOAccountID : commandData .GrantOutput .RdsDatabase . AccountId ,
274258 SSORoleName : grant .Grant .Id ,
275- SSORegion : commandData .GrantOutput .SSORegion ,
276- SSOStartURL : commandData .GrantOutput .SSOStartURL ,
259+ SSORegion : commandData .GrantOutput .SsoRegion ,
260+ SSOStartURL : commandData .GrantOutput .SsoStartUrl ,
277261 },
278262 Initialised : true ,
279263 }
@@ -295,7 +279,7 @@ var proxyCommand = cli.Command{
295279 if err != nil {
296280 return err
297281 }
298- awscfg .Region = commandData .GrantOutput .Database .Region
282+ awscfg .Region = commandData .GrantOutput .RdsDatabase .Region
299283 ssmClient := ssm .NewFromConfig (awscfg )
300284
301285 // listen for interrupt signals and forward them on
@@ -322,7 +306,7 @@ var proxyCommand = cli.Command{
322306 } else {
323307 documentName := "AWS-StartPortForwardingSession"
324308 startSessionInput := ssm.StartSessionInput {
325- Target : & commandData .GrantOutput .SSMSessionTarget ,
309+ Target : & commandData .GrantOutput .SsmSessionTarget ,
326310 DocumentName : & documentName ,
327311 Parameters : map [string ][]string {
328312 "portNumber" : {commandData .SSMPortForwardServerPort },
@@ -438,17 +422,17 @@ var proxyCommand = cli.Command{
438422 // the passwords are always 'password' while the username and database will match that of the target being connected to
439423 var connectionString , cliString , port string
440424 yellow := color .New (color .FgYellow )
441- switch commandData .GrantOutput .Database .Engine {
425+ switch commandData .GrantOutput .RdsDatabase .Engine {
442426 case "postgres" , "aurora-postgresql" :
443427 port = grab .If (overridePort != 0 , strconv .Itoa (overridePort ), "5432" )
444- connectionString = yellow .Sprintf ("postgresql://%s:password@127.0.0.1:%s/%s?sslmode=disable" , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .Database .Database )
445- cliString = yellow .Sprintf (`psql "postgresql://%s:password@127.0.0.1:%s/%s?sslmode=disable"` , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .Database .Database )
428+ connectionString = yellow .Sprintf ("postgresql://%s:password@127.0.0.1:%s/%s?sslmode=disable" , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .RdsDatabase .Database )
429+ cliString = yellow .Sprintf (`psql "postgresql://%s:password@127.0.0.1:%s/%s?sslmode=disable"` , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .RdsDatabase .Database )
446430 case "mysql" , "aurora-mysql" :
447431 port = grab .If (overridePort != 0 , strconv .Itoa (overridePort ), "3306" )
448- connectionString = yellow .Sprintf ("%s:password@tcp(127.0.0.1:%s)/%s" , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .Database .Database )
449- cliString = yellow .Sprintf (`mysql -u %s -p'password' -h 127.0.0.1 -P %s %s` , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .Database .Database )
432+ connectionString = yellow .Sprintf ("%s:password@tcp(127.0.0.1:%s)/%s" , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .RdsDatabase .Database )
433+ cliString = yellow .Sprintf (`mysql -u %s -p'password' -h 127.0.0.1 -P %s %s` , commandData .GrantOutput .User .Username , port , commandData .GrantOutput .RdsDatabase .Database )
450434 default :
451- return fmt .Errorf ("unsupported database engine: %s, maybe you need to update your `cf` cli" , commandData .GrantOutput .Database .Engine )
435+ return fmt .Errorf ("unsupported database engine: %s, maybe you need to update your `cf` cli" , commandData .GrantOutput .RdsDatabase .Engine )
452436 }
453437
454438 clio .NewLine ()
@@ -458,7 +442,7 @@ var proxyCommand = cli.Command{
458442 clio .Infof ("You can connect now using this connection string: %s" , connectionString )
459443 clio .NewLine ()
460444
461- clio .Infof ("Or using the %s cli: %s" , commandData .GrantOutput .Database .Engine , cliString )
445+ clio .Infof ("Or using the %s cli: %s" , commandData .GrantOutput .RdsDatabase .Engine , cliString )
462446 clio .NewLine ()
463447
464448 defer cancel ()
@@ -570,7 +554,7 @@ func GrabUnusedPort() (string, error) {
570554}
571555
572556type CommandData struct {
573- GrantOutput AWSRDS
557+ GrantOutput * accessv1alpha1. AWSRDSOutput
574558 SSMPortForwardLocalPort string
575559 SSMPortForwardServerPort string
576560}
0 commit comments