@@ -250,7 +250,7 @@ var (
250250 EventTypeRunFinish EventType = "runFinish"
251251)
252252
253- func getContextInput (prg * types.Program , ref types.ToolReference , input string ) (string , error ) {
253+ func getToolRefInput (prg * types.Program , ref types.ToolReference , input string ) (string , error ) {
254254 if ref .Arg == "" {
255255 return "" , nil
256256 }
@@ -355,7 +355,7 @@ func (r *Runner) getContext(callCtx engine.Context, state *State, monitor Monito
355355 continue
356356 }
357357
358- contextInput , err := getContextInput (callCtx .Program , toolRef , input )
358+ contextInput , err := getToolRefInput (callCtx .Program , toolRef , input )
359359 if err != nil {
360360 return nil , nil , err
361361 }
@@ -867,7 +867,7 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
867867 }
868868
869869 var (
870- cred * credentials.Credential
870+ c * credentials.Credential
871871 exists bool
872872 )
873873
@@ -879,25 +879,39 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
879879 // Only try to look up the cred if the tool is on GitHub or has an alias.
880880 // If it is a GitHub tool and has an alias, the alias overrides the tool name, so we use it as the credential name.
881881 if isGitHubTool (toolName ) && credentialAlias == "" {
882- cred , exists , err = r .credStore .Get (toolName )
882+ c , exists , err = r .credStore .Get (toolName )
883883 if err != nil {
884884 return nil , fmt .Errorf ("failed to get credentials for tool %s: %w" , toolName , err )
885885 }
886886 } else if credentialAlias != "" {
887- cred , exists , err = r .credStore .Get (credentialAlias )
887+ c , exists , err = r .credStore .Get (credentialAlias )
888888 if err != nil {
889889 return nil , fmt .Errorf ("failed to get credentials for tool %s: %w" , credentialAlias , err )
890890 }
891891 }
892892
893+ if c == nil {
894+ c = & credentials.Credential {}
895+ }
896+
893897 // If the credential doesn't already exist in the store, run the credential tool in order to get the value,
894898 // and save it in the store.
895- if ! exists {
899+ if ! exists || c . IsExpired () {
896900 credToolRefs , ok := callCtx .Tool .ToolMapping [credToolName ]
897901 if ! ok || len (credToolRefs ) != 1 {
898902 return nil , fmt .Errorf ("failed to find ID for tool %s" , credToolName )
899903 }
900904
905+ // If the existing credential is expired, we need to provide it to the cred tool through the environment.
906+ if exists && c .IsExpired () {
907+ credJSON , err := json .Marshal (c )
908+ if err != nil {
909+ return nil , fmt .Errorf ("failed to marshal credential: %w" , err )
910+ }
911+ env = append (env , fmt .Sprintf ("%s=%s" , credentials .ExistingCredential , string (credJSON )))
912+ }
913+
914+ // Get the input for the credential tool, if there is any.
901915 var input string
902916 if args != nil {
903917 inputBytes , err := json .Marshal (args )
@@ -916,21 +930,14 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
916930 return nil , fmt .Errorf ("invalid state: credential tool [%s] can not result in a continuation" , credToolName )
917931 }
918932
919- var envMap struct {
920- Env map [string ]string `json:"env"`
921- }
922- if err := json .Unmarshal ([]byte (* res .Result ), & envMap ); err != nil {
933+ if err := json .Unmarshal ([]byte (* res .Result ), & c ); err != nil {
923934 return nil , fmt .Errorf ("failed to unmarshal credential tool %s response: %w" , credToolName , err )
924935 }
925-
926- cred = & credentials.Credential {
927- Type : credentials .CredentialTypeTool ,
928- Env : envMap .Env ,
929- ToolName : credName ,
930- }
936+ c .ToolName = credName
937+ c .Type = credentials .CredentialTypeTool
931938
932939 isEmpty := true
933- for _ , v := range cred .Env {
940+ for _ , v := range c .Env {
934941 if v != "" {
935942 isEmpty = false
936943 break
@@ -941,15 +948,15 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
941948 if (isGitHubTool (toolName ) && callCtx .Program .ToolSet [credToolRefs [0 ].ToolID ].Source .Repo != nil ) || credentialAlias != "" {
942949 if isEmpty {
943950 log .Warnf ("Not saving empty credential for tool %s" , toolName )
944- } else if err := r .credStore .Add (* cred ); err != nil {
951+ } else if err := r .credStore .Add (* c ); err != nil {
945952 return nil , fmt .Errorf ("failed to add credential for tool %s: %w" , toolName , err )
946953 }
947954 } else {
948955 log .Warnf ("Not saving credential for tool %s - credentials will only be saved for tools from GitHub, or tools that use aliases." , toolName )
949956 }
950957 }
951958
952- for k , v := range cred .Env {
959+ for k , v := range c .Env {
953960 env = append (env , fmt .Sprintf ("%s=%s" , k , v ))
954961 }
955962 }
0 commit comments