@@ -250,7 +250,7 @@ var (
250
250
EventTypeRunFinish EventType = "runFinish"
251
251
)
252
252
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 ) {
254
254
if ref .Arg == "" {
255
255
return "" , nil
256
256
}
@@ -355,7 +355,7 @@ func (r *Runner) getContext(callCtx engine.Context, state *State, monitor Monito
355
355
continue
356
356
}
357
357
358
- contextInput , err := getContextInput (callCtx .Program , toolRef , input )
358
+ contextInput , err := getToolRefInput (callCtx .Program , toolRef , input )
359
359
if err != nil {
360
360
return nil , nil , err
361
361
}
@@ -867,7 +867,7 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
867
867
}
868
868
869
869
var (
870
- cred * credentials.Credential
870
+ c * credentials.Credential
871
871
exists bool
872
872
)
873
873
@@ -879,25 +879,39 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
879
879
// Only try to look up the cred if the tool is on GitHub or has an alias.
880
880
// If it is a GitHub tool and has an alias, the alias overrides the tool name, so we use it as the credential name.
881
881
if isGitHubTool (toolName ) && credentialAlias == "" {
882
- cred , exists , err = r .credStore .Get (toolName )
882
+ c , exists , err = r .credStore .Get (toolName )
883
883
if err != nil {
884
884
return nil , fmt .Errorf ("failed to get credentials for tool %s: %w" , toolName , err )
885
885
}
886
886
} else if credentialAlias != "" {
887
- cred , exists , err = r .credStore .Get (credentialAlias )
887
+ c , exists , err = r .credStore .Get (credentialAlias )
888
888
if err != nil {
889
889
return nil , fmt .Errorf ("failed to get credentials for tool %s: %w" , credentialAlias , err )
890
890
}
891
891
}
892
892
893
+ if c == nil {
894
+ c = & credentials.Credential {}
895
+ }
896
+
893
897
// If the credential doesn't already exist in the store, run the credential tool in order to get the value,
894
898
// and save it in the store.
895
- if ! exists {
899
+ if ! exists || c . IsExpired () {
896
900
credToolRefs , ok := callCtx .Tool .ToolMapping [credToolName ]
897
901
if ! ok || len (credToolRefs ) != 1 {
898
902
return nil , fmt .Errorf ("failed to find ID for tool %s" , credToolName )
899
903
}
900
904
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.
901
915
var input string
902
916
if args != nil {
903
917
inputBytes , err := json .Marshal (args )
@@ -916,21 +930,14 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
916
930
return nil , fmt .Errorf ("invalid state: credential tool [%s] can not result in a continuation" , credToolName )
917
931
}
918
932
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 {
923
934
return nil , fmt .Errorf ("failed to unmarshal credential tool %s response: %w" , credToolName , err )
924
935
}
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
931
938
932
939
isEmpty := true
933
- for _ , v := range cred .Env {
940
+ for _ , v := range c .Env {
934
941
if v != "" {
935
942
isEmpty = false
936
943
break
@@ -941,15 +948,15 @@ func (r *Runner) handleCredentials(callCtx engine.Context, monitor Monitor, env
941
948
if (isGitHubTool (toolName ) && callCtx .Program .ToolSet [credToolRefs [0 ].ToolID ].Source .Repo != nil ) || credentialAlias != "" {
942
949
if isEmpty {
943
950
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 {
945
952
return nil , fmt .Errorf ("failed to add credential for tool %s: %w" , toolName , err )
946
953
}
947
954
} else {
948
955
log .Warnf ("Not saving credential for tool %s - credentials will only be saved for tools from GitHub, or tools that use aliases." , toolName )
949
956
}
950
957
}
951
958
952
- for k , v := range cred .Env {
959
+ for k , v := range c .Env {
953
960
env = append (env , fmt .Sprintf ("%s=%s" , k , v ))
954
961
}
955
962
}
0 commit comments