Skip to content

Add secrets #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion aggregator/task_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func (agg *Aggregator) startTaskEngine(ctx context.Context) {
})
agg.worker = apqueue.NewWorker(agg.queue, agg.db)
taskExecutor := taskengine.NewExecutor(agg.db, agg.logger)
taskengine.SetMacro(agg.config.Macros)
taskengine.SetMacroVars(agg.config.MacroVars)
taskengine.SetMacroSecrets(agg.config.MacroSecrets)
taskengine.SetCache(agg.cache)
macros.SetRpc(agg.config.SmartWallet.EthRpcUrl)

Expand Down
14 changes: 8 additions & 6 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ type Config struct {
SocketPath string
Environment sdklogging.LogLevel

Macros map[string]string
MacroVars map[string]string
MacroSecrets map[string]string

MetricsReg *prometheus.Registry
MetricsReg *prometheus.Registry
}

type SmartWalletConfig struct {
Expand Down Expand Up @@ -96,7 +97,7 @@ type ConfigRaw struct {

SocketPath string `yaml:"socket_path"`

Macros map[string]string `yaml:"macros"`
Macros map[string]map[string]string `yaml:"macros"`
}

// These are read from CredibleSquaringDeploymentFileFlag
Expand Down Expand Up @@ -202,9 +203,10 @@ func NewConfig(configFilePath string) (*Config, error) {
ControllerPrivateKey: controllerPrivateKey,
},

SocketPath: configRaw.SocketPath,
Macros: configRaw.Macros,
MetricsReg: reg,
SocketPath: configRaw.SocketPath,
MacroVars: configRaw.Macros["vars"],
MacroSecrets: configRaw.Macros["secrets"],
MetricsReg: reg,
}

if config.SocketPath == "" {
Expand Down
13 changes: 9 additions & 4 deletions core/taskengine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ var (

// a global variable that we expose to our tasks. User can use `{{name}}` to access them
// These macro are define in our aggregator yaml config file under `macros`
macroEnvs map[string]string
cache *bigcache.BigCache
macroVars map[string]string
macroSecrets map[string]string
cache *bigcache.BigCache

defaultSalt = big.NewInt(0)
)
Expand All @@ -56,8 +57,12 @@ func SetLogger(mylogger sdklogging.Logger) {
}

// Set the global macro system. macros are static, immutable and available to all tasks at runtime
func SetMacro(v map[string]string) {
macroEnvs = v
func SetMacroVars(v map[string]string) {
macroVars = v
}

func SetMacroSecrets(v map[string]string) {
macroSecrets = v
}

func SetCache(c *bigcache.BigCache) {
Expand Down
12 changes: 11 additions & 1 deletion core/taskengine/macros/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ func Render(text []byte, vars map[string]string) string {
return RenderString(string(text), vars)
}

// TODO: Add more variable and coument these macros
// TODO: Add more variable and documents these macros
func RenderString(text string, vars map[string]string) string {
for k, v := range vars {
text = strings.ReplaceAll(text, fmt.Sprintf("{{%s}}", k), v)
}

return text
}

// TODO: document all of our available secrets
// There is a certain operation we let use use it, but don't let user see it. Example to setup email or notifiction, behind the scene, they require an API key. So they can use their API key to send notification and craft the message the way they want, but they cannot see it.
func RenderSecrets(text string, vars map[string]string) string {
for k, v := range vars {
text = strings.ReplaceAll(text, fmt.Sprintf("${{secrets.%s}}", k), v)
}

return text
}
15 changes: 15 additions & 0 deletions core/taskengine/macros/vars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package macros

import (
"testing"
)

func TestRenderSecret(t *testing.T) {
text := RenderSecrets("this has ${{secrets.foo_token}}", map[string]string{
"foo_token": "123abc",
})

if text != "this has 123abc" {
t.Errorf("render secret doesn't render final text that contains the secrets. expect `this has 123abc` but got %s", text)
}
}
2 changes: 1 addition & 1 deletion core/taskengine/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (v *VM) runRestApi(stepID string, nodeValue *avsproto.RestAPINode) (*avspro
// only evaluate string when there is string interpolation
if nodeValue.Body != "" && (strings.Contains(nodeValue.Body, "$") || strings.Contains(nodeValue.Body, "`")) {
nodeValue2 := &avsproto.RestAPINode{
Url: macros.RenderString(nodeValue.Url, macroEnvs),
Url: macros.RenderSecrets(nodeValue.Url, macroSecrets),
Headers: nodeValue.Headers,
Method: nodeValue.Method,
Body: strings.Clone(nodeValue.Body),
Expand Down
Loading