From f3ee556b3f17147f8f3eaf603d6cf1bcd7a26e39 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Sat, 24 Aug 2019 12:21:42 +0200 Subject: [PATCH] Make corredor calls more configurable JWT is now sent as part of the Config prop, along with msg/sys/cmp base api URLs This still needs to be configured through env: - CORREDOR_API_BASE_URL_SYSTEM - CORREDOR_API_BASE_URL_MESSAGING - CORREDOR_API_BASE_URL_COMPOSE --- compose/internal/service/automation_runner.go | 22 +++- compose/internal/service/service.go | 10 +- compose/proto/script_runner.pb.go | 122 ++++++++++-------- pkg/cli/options/corredor.go | 6 +- 4 files changed, 98 insertions(+), 62 deletions(-) diff --git a/compose/internal/service/automation_runner.go b/compose/internal/service/automation_runner.go index 639371da2..44eac810f 100644 --- a/compose/internal/service/automation_runner.go +++ b/compose/internal/service/automation_runner.go @@ -19,6 +19,7 @@ import ( type ( automationRunner struct { + opt AutomationRunnerOpt ac automationRunnerAccessControler logger *zap.Logger runner proto.ScriptRunnerClient @@ -34,14 +35,22 @@ type ( automationRunnerAccessControler interface { CanRunAutomationTrigger(ctx context.Context, r *automation.Trigger) bool } + + AutomationRunnerOpt struct { + ApiBaseURLSystem string + ApiBaseURLMessaging string + ApiBaseURLCompose string + } ) const ( AutomationResourceRecord = "compose:record" ) -func AutomationRunner(f automationScriptsFinder, r proto.ScriptRunnerClient) automationRunner { +func AutomationRunner(opt AutomationRunnerOpt, f automationScriptsFinder, r proto.ScriptRunnerClient) automationRunner { var svc = automationRunner{ + opt: opt, + ac: DefaultAccessControl, scriptFinder: f, @@ -224,14 +233,21 @@ func (svc automationRunner) makeRecordScriptRunner(ctx context.Context, ns *type } // This could be executed in a goroutine (by *after triggers, - // so we need ot rewire the sentry panic recoverty + // so we need to rewire the sentry panic recovery defer sentry.Recover() ctx, cancelFn := context.WithTimeout(ctx, time.Second*5) defer cancelFn() // Add invoker's or defined credentials/jwt - req.JWT = svc.getJWT(ctx, script) + req.Config = map[string]string{ + "api.jwt": svc.getJWT(ctx, script), + + // Let the script know where the API is + "api.baseURL.system": svc.opt.ApiBaseURLSystem, + "api.baseURL.compose": svc.opt.ApiBaseURLCompose, + "api.baseURL.messaging": svc.opt.ApiBaseURLMessaging, + } // Add script info req.Script = proto.FromAutomationScript(script) diff --git a/compose/internal/service/service.go b/compose/internal/service/service.go index 9f7649100..bee3dced5 100644 --- a/compose/internal/service/service.go +++ b/compose/internal/service/service.go @@ -117,7 +117,15 @@ func Init(ctx context.Context, log *zap.Logger, c Config) (err error) { scriptRunnerClient = proto.NewScriptRunnerClient(corredor) } - DefaultAutomationRunner = AutomationRunner(ias, scriptRunnerClient) + DefaultAutomationRunner = AutomationRunner( + AutomationRunnerOpt{ + ApiBaseURLSystem: c.Corredor.ApiBaseURLSystem, + ApiBaseURLMessaging: c.Corredor.ApiBaseURLMessaging, + ApiBaseURLCompose: c.Corredor.ApiBaseURLCompose, + }, + ias, + scriptRunnerClient, + ) } // Compose internals: diff --git a/compose/proto/script_runner.pb.go b/compose/proto/script_runner.pb.go index 3d8c8b55b..10ebff23d 100644 --- a/compose/proto/script_runner.pb.go +++ b/compose/proto/script_runner.pb.go @@ -72,12 +72,12 @@ func (m *RunTestRequest) GetName() string { } type RunNamespaceRequest struct { - JWT string `protobuf:"bytes,1,opt,name=JWT,proto3" json:"JWT,omitempty"` - Script *Script `protobuf:"bytes,2,opt,name=script,proto3" json:"script,omitempty"` - Namespace *Namespace `protobuf:"bytes,3,opt,name=namespace,proto3" json:"namespace,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Config map[string]string `protobuf:"bytes,1,rep,name=config,proto3" json:"config,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + Script *Script `protobuf:"bytes,2,opt,name=script,proto3" json:"script,omitempty"` + Namespace *Namespace `protobuf:"bytes,3,opt,name=namespace,proto3" json:"namespace,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *RunNamespaceRequest) Reset() { *m = RunNamespaceRequest{} } @@ -105,11 +105,11 @@ func (m *RunNamespaceRequest) XXX_DiscardUnknown() { var xxx_messageInfo_RunNamespaceRequest proto.InternalMessageInfo -func (m *RunNamespaceRequest) GetJWT() string { +func (m *RunNamespaceRequest) GetConfig() map[string]string { if m != nil { - return m.JWT + return m.Config } - return "" + return nil } func (m *RunNamespaceRequest) GetScript() *Script { @@ -127,13 +127,13 @@ func (m *RunNamespaceRequest) GetNamespace() *Namespace { } type RunModuleRequest struct { - JWT string `protobuf:"bytes,1,opt,name=JWT,proto3" json:"JWT,omitempty"` - Script *Script `protobuf:"bytes,2,opt,name=script,proto3" json:"script,omitempty"` - Namespace *Namespace `protobuf:"bytes,3,opt,name=namespace,proto3" json:"namespace,omitempty"` - Module *Module `protobuf:"bytes,4,opt,name=module,proto3" json:"module,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Config map[string]string `protobuf:"bytes,1,rep,name=config,proto3" json:"config,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + Script *Script `protobuf:"bytes,2,opt,name=script,proto3" json:"script,omitempty"` + Namespace *Namespace `protobuf:"bytes,3,opt,name=namespace,proto3" json:"namespace,omitempty"` + Module *Module `protobuf:"bytes,4,opt,name=module,proto3" json:"module,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *RunModuleRequest) Reset() { *m = RunModuleRequest{} } @@ -161,11 +161,11 @@ func (m *RunModuleRequest) XXX_DiscardUnknown() { var xxx_messageInfo_RunModuleRequest proto.InternalMessageInfo -func (m *RunModuleRequest) GetJWT() string { +func (m *RunModuleRequest) GetConfig() map[string]string { if m != nil { - return m.JWT + return m.Config } - return "" + return nil } func (m *RunModuleRequest) GetScript() *Script { @@ -190,14 +190,14 @@ func (m *RunModuleRequest) GetModule() *Module { } type RunRecordRequest struct { - JWT string `protobuf:"bytes,1,opt,name=JWT,proto3" json:"JWT,omitempty"` - Script *Script `protobuf:"bytes,2,opt,name=script,proto3" json:"script,omitempty"` - Namespace *Namespace `protobuf:"bytes,3,opt,name=namespace,proto3" json:"namespace,omitempty"` - Module *Module `protobuf:"bytes,4,opt,name=module,proto3" json:"module,omitempty"` - Record *Record `protobuf:"bytes,5,opt,name=record,proto3" json:"record,omitempty"` - XXX_NoUnkeyedLiteral struct{} `json:"-"` - XXX_unrecognized []byte `json:"-"` - XXX_sizecache int32 `json:"-"` + Config map[string]string `protobuf:"bytes,1,rep,name=config,proto3" json:"config,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"` + Script *Script `protobuf:"bytes,2,opt,name=script,proto3" json:"script,omitempty"` + Namespace *Namespace `protobuf:"bytes,3,opt,name=namespace,proto3" json:"namespace,omitempty"` + Module *Module `protobuf:"bytes,4,opt,name=module,proto3" json:"module,omitempty"` + Record *Record `protobuf:"bytes,5,opt,name=record,proto3" json:"record,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` } func (m *RunRecordRequest) Reset() { *m = RunRecordRequest{} } @@ -225,11 +225,11 @@ func (m *RunRecordRequest) XXX_DiscardUnknown() { var xxx_messageInfo_RunRecordRequest proto.InternalMessageInfo -func (m *RunRecordRequest) GetJWT() string { +func (m *RunRecordRequest) GetConfig() map[string]string { if m != nil { - return m.JWT + return m.Config } - return "" + return nil } func (m *RunRecordRequest) GetScript() *Script { @@ -474,8 +474,11 @@ func (m *Script) GetAsync() bool { func init() { proto.RegisterType((*RunTestRequest)(nil), "compose.RunTestRequest") proto.RegisterType((*RunNamespaceRequest)(nil), "compose.RunNamespaceRequest") + proto.RegisterMapType((map[string]string)(nil), "compose.RunNamespaceRequest.ConfigEntry") proto.RegisterType((*RunModuleRequest)(nil), "compose.RunModuleRequest") + proto.RegisterMapType((map[string]string)(nil), "compose.RunModuleRequest.ConfigEntry") proto.RegisterType((*RunRecordRequest)(nil), "compose.RunRecordRequest") + proto.RegisterMapType((map[string]string)(nil), "compose.RunRecordRequest.ConfigEntry") proto.RegisterType((*RunTestResponse)(nil), "compose.RunTestResponse") proto.RegisterType((*RunNamespaceResponse)(nil), "compose.RunNamespaceResponse") proto.RegisterType((*RunModuleResponse)(nil), "compose.RunModuleResponse") @@ -486,33 +489,38 @@ func init() { func init() { proto.RegisterFile("script_runner.proto", fileDescriptor_62210b9e3e4a7a06) } var fileDescriptor_62210b9e3e4a7a06 = []byte{ - // 414 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x54, 0xcd, 0x4a, 0x2b, 0x31, - 0x14, 0x26, 0xfd, 0x99, 0xde, 0x9e, 0xdb, 0x7b, 0xdb, 0xa6, 0x45, 0xe3, 0xa0, 0x20, 0xb3, 0xd1, - 0x55, 0x91, 0xba, 0x12, 0x0a, 0x82, 0xab, 0x22, 0xe8, 0x22, 0x16, 0x04, 0x37, 0x52, 0xa7, 0x59, - 0x14, 0x9c, 0x64, 0x4c, 0x26, 0x0b, 0xb7, 0xbe, 0x83, 0xef, 0xe0, 0xeb, 0xf8, 0x46, 0x32, 0x49, - 0x66, 0xa6, 0xa9, 0x5a, 0x74, 0xa5, 0xab, 0xce, 0xc9, 0xf9, 0xf2, 0xe5, 0x3b, 0xdf, 0x39, 0xa7, - 0x30, 0x50, 0xb1, 0x5c, 0xa6, 0xd9, 0xad, 0xd4, 0x9c, 0x33, 0x39, 0x4a, 0xa5, 0xc8, 0x04, 0x6e, - 0xc5, 0x22, 0x49, 0x85, 0x62, 0x61, 0x97, 0xcf, 0x13, 0xa6, 0x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 0x15, 0x86, 0x36, 0x17, 0xb6, 0x80, 0xc0, 0xda, 0xfd, 0x9d, 0x95, 0xc0, 0x04, 0x5a, 0xd9, 0x32, - 0x61, 0x42, 0x67, 0xa6, 0x21, 0xff, 0x68, 0x11, 0xe2, 0x21, 0x34, 0xe7, 0xea, 0x91, 0xc7, 0xc6, - 0xf9, 0x3f, 0xd4, 0x06, 0xe3, 0xe7, 0x1a, 0x74, 0x5c, 0x57, 0xcd, 0x36, 0xe3, 0x13, 0x68, 0xe4, - 0x66, 0xe2, 0xed, 0x4a, 0x97, 0xb7, 0xa0, 0x21, 0x79, 0x9f, 0x70, 0xa5, 0x4d, 0xa1, 0x5d, 0xba, - 0x88, 0x77, 0x57, 0x61, 0xeb, 0x1b, 0x1a, 0xee, 0x7d, 0x92, 0x75, 0x4c, 0xa7, 0x10, 0x58, 0x2f, - 0xf1, 0xce, 0x2a, 0xd0, 0xdb, 0xb1, 0x30, 0xfc, 0x28, 0x55, 0x11, 0x58, 0x3b, 0x7d, 0x02, 0x6f, - 0xf2, 0x7d, 0x02, 0xbf, 0x4d, 0x67, 0xad, 0x9b, 0xa6, 0xf9, 0x87, 0xba, 0x0b, 0xcc, 0xcf, 0xf1, - 0x5b, 0x00, 0x00, 0x00, 0xff, 0xff, 0x88, 0x24, 0x92, 0xac, 0xf5, 0x04, 0x00, 0x00, + // 483 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x54, 0xdd, 0x8a, 0x13, 0x31, + 0x14, 0x26, 0xd3, 0x76, 0x6a, 0x4f, 0x57, 0xbb, 0x9b, 0x5d, 0x34, 0x0e, 0x0a, 0xcb, 0x80, 0xd8, + 0xab, 0x41, 0xea, 0x8d, 0x2b, 0x2b, 0x8a, 0x22, 0xec, 0x8d, 0x5e, 0x44, 0xaf, 0xbc, 0x91, 0x71, + 0x1a, 0xa5, 0xd8, 0x49, 0xc6, 0x64, 0x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b, 0xe1, 0xa4, 0x5d, 0xc1, 0xa8, 0x71, 0x11, 0xdf, 0xdb, 0x35, 0xae, 0xa2, 0xfb, 0x5b, + 0xb2, 0x8e, 0xe9, 0x39, 0x84, 0xd6, 0x4b, 0x7c, 0x77, 0xeb, 0x20, 0x88, 0xa2, 0x4d, 0xa9, 0x96, + 0xc0, 0xda, 0xe9, 0x13, 0x78, 0x6d, 0xe3, 0x13, 0xf8, 0x65, 0x7a, 0x39, 0xfc, 0x30, 0x30, 0xc3, + 0xfd, 0x53, 0x68, 0x3e, 0x8f, 0xff, 0x04, 0x00, 0x00, 0xff, 0xff, 0x1b, 0x6d, 0xe8, 0xeb, 0x30, + 0x06, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/pkg/cli/options/corredor.go b/pkg/cli/options/corredor.go index ee5b145ba..bf80cabda 100644 --- a/pkg/cli/options/corredor.go +++ b/pkg/cli/options/corredor.go @@ -15,7 +15,11 @@ type ( Log bool `env:"CORREDOR_LOG_ENABLED"` MaxBackoffDelay time.Duration `env:"CORREDOR_MAX_BACKOFF_DELAY"` - Log bool `env:"CORREDOR_LOG_ENABLED"` + + // @todo do autodiscovery & prefill these with values we know + ApiBaseURLSystem string `env:"CORREDOR_API_BASE_URL_SYSTEM"` + ApiBaseURLMessaging string `env:"CORREDOR_API_BASE_URL_MESSAGING"` + ApiBaseURLCompose string `env:"CORREDOR_API_BASE_URL_COMPOSE"` } )