@@ -900,6 +900,23 @@ func secretTemplateFunction(accessToken string, currentEtag *string) func(string
900900 }
901901}
902902
903+ func secretTemplateByProjectSlugFunction (accessToken string , currentEtag * string ) func (string , string , string , ... string ) ([]models.SingleEnvironmentVariable , error ) {
904+ return func (projectSlug , envSlug , secretPath string , args ... string ) ([]models.SingleEnvironmentVariable , error ) {
905+ httpClient , err := util .GetRestyClientWithCustomHeaders ()
906+ if err != nil {
907+ return nil , fmt .Errorf ("failed to create HTTP client: %v" , err )
908+ }
909+ httpClient .SetAuthToken (accessToken )
910+
911+ project , err := api .CallGetProjectBySlug (httpClient , projectSlug )
912+ if err != nil {
913+ return nil , fmt .Errorf ("failed to get project by slug: %v" , err )
914+ }
915+
916+ return secretTemplateFunction (accessToken , currentEtag )(project .ID , envSlug , secretPath , args ... )
917+ }
918+ }
919+
903920func getSingleSecretTemplateFunction (accessToken string , currentEtag * string ) func (string , string , string , string ) (models.SingleEnvironmentVariable , error ) {
904921 return func (projectID , envSlug , secretPath , secretName string ) (models.SingleEnvironmentVariable , error ) {
905922 secret , etag , err := util .GetSinglePlainTextSecretByNameV3 (accessToken , projectID , envSlug , secretPath , secretName )
@@ -975,21 +992,31 @@ func dynamicSecretTemplateFunction(accessToken string, dynamicSecretManager *Dyn
975992 }
976993}
977994
978- func ProcessTemplate ( templateId int , templatePath string , data interface {}, accessToken string , currentEtag * string , dynamicSecretManager * DynamicSecretLeaseManager , agentManager * AgentManager ) ( * bytes. Buffer , error ) {
995+ func newTemplateFunctions ( accessToken string , currentEtag * string , dynamicSecretManager * DynamicSecretLeaseManager , agentManager * AgentManager , templateId int ) template. FuncMap {
979996
980- // custom template function to fetch secrets from Infisical
981997 secretFunction := secretTemplateFunction (accessToken , currentEtag )
998+ secretByProjectSlugFunction := secretTemplateByProjectSlugFunction (accessToken , currentEtag )
982999 dynamicSecretFunction := dynamicSecretTemplateFunction (accessToken , dynamicSecretManager , agentManager , templateId , currentEtag )
9831000 getSingleSecretFunction := getSingleSecretTemplateFunction (accessToken , currentEtag )
1001+
9841002 funcs := template.FuncMap {
985- "secret" : secretFunction , // depreciated
986- "listSecrets" : secretFunction ,
987- "dynamic_secret" : dynamicSecretFunction ,
988- "getSecretByName" : getSingleSecretFunction ,
1003+ "secret" : secretFunction , // deprecated
1004+ "dynamic_secret" : dynamicSecretFunction , // deprecated
1005+
1006+ "listSecrets" : secretFunction ,
1007+ "listSecretsByProjectSlug" : secretByProjectSlugFunction ,
1008+ "getSecretByName" : getSingleSecretFunction ,
1009+ "dynamicSecret" : dynamicSecretFunction ,
9891010 }
9901011
1012+ return funcs
1013+ }
1014+
1015+ func ProcessTemplate (templateId int , templatePath string , data interface {}, accessToken string , currentEtag * string , dynamicSecretManager * DynamicSecretLeaseManager , agentManager * AgentManager ) (* bytes.Buffer , error ) {
1016+ templateFunctions := newTemplateFunctions (accessToken , currentEtag , dynamicSecretManager , agentManager , templateId )
1017+
9911018 templateName := path .Base (templatePath )
992- tmpl , err := template .New (templateName ).Funcs (templates .CompileTemplateFunctions (funcs )).ParseFiles (templatePath )
1019+ tmpl , err := template .New (templateName ).Funcs (templates .CompileTemplateFunctions (templateFunctions )).ParseFiles (templatePath )
9931020 if err != nil {
9941021 return nil , err
9951022 }
@@ -1011,18 +1038,10 @@ func ProcessBase64Template(templateId int, encodedTemplate string, data interfac
10111038
10121039 templateString := string (decoded )
10131040
1014- secretFunction := secretTemplateFunction (accessToken , currentEtag ) // TODO: Fix this
1015- dynamicSecretFunction := dynamicSecretTemplateFunction (accessToken , dynamicSecretLeaseManager , agentManager , templateId , currentEtag )
1016- getSingleSecretFunction := getSingleSecretTemplateFunction (accessToken , currentEtag )
1017- funcs := template.FuncMap {
1018- "secret" : secretFunction ,
1019- "dynamic_secret" : dynamicSecretFunction ,
1020- "getSecretByName" : getSingleSecretFunction ,
1021- }
1022-
10231041 templateName := "base64Template"
1042+ templateFunctions := newTemplateFunctions (accessToken , currentEtag , dynamicSecretLeaseManager , agentManager , templateId )
10241043
1025- tmpl , err := template .New (templateName ).Funcs (templates .CompileTemplateFunctions (funcs )).Parse (templateString )
1044+ tmpl , err := template .New (templateName ).Funcs (templates .CompileTemplateFunctions (templateFunctions )).Parse (templateString )
10261045 if err != nil {
10271046 return nil , err
10281047 }
@@ -1037,18 +1056,11 @@ func ProcessBase64Template(templateId int, encodedTemplate string, data interfac
10371056
10381057func ProcessLiteralTemplate (templateId int , templateString string , data interface {}, accessToken string , currentEtag * string , dynamicSecretLeaseManager * DynamicSecretLeaseManager , agentManager * AgentManager ) (* bytes.Buffer , error ) {
10391058
1040- secretFunction := secretTemplateFunction (accessToken , currentEtag )
1041- dynamicSecretFunction := dynamicSecretTemplateFunction (accessToken , dynamicSecretLeaseManager , agentManager , templateId , currentEtag )
1042- getSingleSecretFunction := getSingleSecretTemplateFunction (accessToken , currentEtag )
1043- funcs := template.FuncMap {
1044- "secret" : secretFunction ,
1045- "dynamic_secret" : dynamicSecretFunction ,
1046- "getSecretByName" : getSingleSecretFunction ,
1047- }
1059+ templateFunctions := newTemplateFunctions (accessToken , currentEtag , dynamicSecretLeaseManager , agentManager , templateId )
10481060
10491061 templateName := "literalTemplate"
10501062
1051- tmpl , err := template .New (templateName ).Funcs (templates .CompileTemplateFunctions (funcs )).Parse (templateString )
1063+ tmpl , err := template .New (templateName ).Funcs (templates .CompileTemplateFunctions (templateFunctions )).Parse (templateString )
10521064 if err != nil {
10531065 return nil , err
10541066 }
0 commit comments