From 9c51fa87a4a3c68ca9e3ef3cdd65280059fa18bd Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Wed, 15 May 2024 13:13:35 -0400 Subject: [PATCH 1/4] feat: support openfail by default implements openfail - if the agent's config is missing the `xmidt_credentials`, then the agent will connect to xmidt via openfail --- cmd/xmidt-agent/main.go | 12 ++++++++---- cmd/xmidt-agent/ws.go | 10 +++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/cmd/xmidt-agent/main.go b/cmd/xmidt-agent/main.go index b8185f7..b490862 100644 --- a/cmd/xmidt-agent/main.go +++ b/cmd/xmidt-agent/main.go @@ -226,10 +226,14 @@ func onStart(cred *credentials.Credentials, ws *websocket.Websocket, qos *qos.Ha return nil } - ctx, cancel := context.WithTimeout(ctx, waitUntilFetched) - defer cancel() - // blocks until an attempt to fetch the credentials has been made or the context is canceled - cred.WaitUntilFetched(ctx) + // in case of openfail, cred will be nil + if cred != nil { + ctx, cancel := context.WithTimeout(ctx, waitUntilFetched) + defer cancel() + // blocks until an attempt to fetch the credentials has been made or the context is canceled + cred.WaitUntilFetched(ctx) + } + ws.Start() qos.Start() diff --git a/cmd/xmidt-agent/ws.go b/cmd/xmidt-agent/ws.go index 4b501e7..50e40bd 100644 --- a/cmd/xmidt-agent/ws.go +++ b/cmd/xmidt-agent/ws.go @@ -6,6 +6,7 @@ package main import ( "context" "errors" + "net/http" "net/url" "time" @@ -58,6 +59,13 @@ func provideWS(in wsIn) (wsOut, error) { fetchURLFunc = in.JWTXT.Endpoint } + credDecorator := func(http.Header) error { return nil } + // Cred is not required + // credDecorator() will default to openfail if in.Cred is nil + if in.Cred != nil { + credDecorator = in.Cred.Decorate + } + client, err := in.Websocket.HTTPClient.NewClient() if err != nil { return wsOut{}, err @@ -76,7 +84,7 @@ func provideWS(in wsIn) (wsOut, error) { websocket.KeepAliveInterval(in.Websocket.KeepAliveInterval), websocket.HTTPClient(client), websocket.MaxMessageBytes(in.Websocket.MaxMessageBytes), - websocket.CredentialsDecorator(in.Cred.Decorate), + websocket.CredentialsDecorator(credDecorator), websocket.ConveyDecorator(in.Metadata.Decorate), websocket.AdditionalHeaders(in.Websocket.AdditionalHeaders), websocket.NowFunc(time.Now), From 941492d7a3ca418eec04510572441d146971e134 Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Wed, 15 May 2024 13:58:08 -0400 Subject: [PATCH 2/4] feat: support openfail by default implements openfail - if the agent's config is missing the `xmidt_credentials`, then the agent will connect to xmidt via openfail --- cmd/xmidt-agent/main.go | 2 +- cmd/xmidt-agent/ws.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/xmidt-agent/main.go b/cmd/xmidt-agent/main.go index b490862..d88cbcb 100644 --- a/cmd/xmidt-agent/main.go +++ b/cmd/xmidt-agent/main.go @@ -226,7 +226,7 @@ func onStart(cred *credentials.Credentials, ws *websocket.Websocket, qos *qos.Ha return nil } - // in case of openfail, cred will be nil + // Agent will openfail if its config section `xmidt_credentials` is omitted (cred will be nil). if cred != nil { ctx, cancel := context.WithTimeout(ctx, waitUntilFetched) defer cancel() diff --git a/cmd/xmidt-agent/ws.go b/cmd/xmidt-agent/ws.go index 50e40bd..4f4c090 100644 --- a/cmd/xmidt-agent/ws.go +++ b/cmd/xmidt-agent/ws.go @@ -61,7 +61,7 @@ func provideWS(in wsIn) (wsOut, error) { credDecorator := func(http.Header) error { return nil } // Cred is not required - // credDecorator() will default to openfail if in.Cred is nil + // Agent will openfail if its config section `xmidt_credentials` is omitted (in.Cred will be nil). if in.Cred != nil { credDecorator = in.Cred.Decorate } From 3dd2c7af255c5aaf5217b442d1d4f76248c9412c Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Wed, 15 May 2024 14:55:00 -0400 Subject: [PATCH 3/4] chore: update pr --- cmd/xmidt-agent/ws.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/cmd/xmidt-agent/ws.go b/cmd/xmidt-agent/ws.go index 4f4c090..776a0e6 100644 --- a/cmd/xmidt-agent/ws.go +++ b/cmd/xmidt-agent/ws.go @@ -6,7 +6,6 @@ package main import ( "context" "errors" - "net/http" "net/url" "time" @@ -59,20 +58,20 @@ func provideWS(in wsIn) (wsOut, error) { fetchURLFunc = in.JWTXT.Endpoint } - credDecorator := func(http.Header) error { return nil } - // Cred is not required - // Agent will openfail if its config section `xmidt_credentials` is omitted (in.Cred will be nil). - if in.Cred != nil { - credDecorator = in.Cred.Decorate - } - client, err := in.Websocket.HTTPClient.NewClient() if err != nil { return wsOut{}, err } + var opts []websocket.Option + // Cred is not required + // Agent will openfail if its config section `xmidt_credentials` is omitted (in.Cred will be nil). + if in.Cred != nil { + opts = append(opts, websocket.CredentialsDecorator(in.Cred.Decorate)) + } + // Configuration options - opts := []websocket.Option{ + opts = append(opts, websocket.DeviceID(in.Identity.DeviceID), websocket.FetchURLTimeout(in.Websocket.FetchURLTimeout), websocket.FetchURL( @@ -84,7 +83,6 @@ func provideWS(in wsIn) (wsOut, error) { websocket.KeepAliveInterval(in.Websocket.KeepAliveInterval), websocket.HTTPClient(client), websocket.MaxMessageBytes(in.Websocket.MaxMessageBytes), - websocket.CredentialsDecorator(credDecorator), websocket.ConveyDecorator(in.Metadata.Decorate), websocket.AdditionalHeaders(in.Websocket.AdditionalHeaders), websocket.NowFunc(time.Now), @@ -92,7 +90,7 @@ func provideWS(in wsIn) (wsOut, error) { websocket.WithIPv4(!in.Websocket.DisableV4), websocket.Once(in.Websocket.Once), websocket.RetryPolicy(in.Websocket.RetryPolicy), - } + ) // Listener options var ( From a9f666f387ad70fe6a318f554a693066aacb3bca Mon Sep 17 00:00:00 2001 From: Owen Cabalceta Date: Wed, 15 May 2024 14:59:11 -0400 Subject: [PATCH 4/4] chore: update docs --- cmd/xmidt-agent/main.go | 2 +- cmd/xmidt-agent/ws.go | 3 +-- internal/websocket/ws.go | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/xmidt-agent/main.go b/cmd/xmidt-agent/main.go index d88cbcb..6bc7fb3 100644 --- a/cmd/xmidt-agent/main.go +++ b/cmd/xmidt-agent/main.go @@ -226,7 +226,7 @@ func onStart(cred *credentials.Credentials, ws *websocket.Websocket, qos *qos.Ha return nil } - // Agent will openfail if its config section `xmidt_credentials` is omitted (cred will be nil). + // Allow operations where no credentials are desired (cred will be nil). if cred != nil { ctx, cancel := context.WithTimeout(ctx, waitUntilFetched) defer cancel() diff --git a/cmd/xmidt-agent/ws.go b/cmd/xmidt-agent/ws.go index 776a0e6..9a85274 100644 --- a/cmd/xmidt-agent/ws.go +++ b/cmd/xmidt-agent/ws.go @@ -64,8 +64,7 @@ func provideWS(in wsIn) (wsOut, error) { } var opts []websocket.Option - // Cred is not required - // Agent will openfail if its config section `xmidt_credentials` is omitted (in.Cred will be nil). + // Allow operations where no credentials are desired (in.Cred will be nil). if in.Cred != nil { opts = append(opts, websocket.CredentialsDecorator(in.Cred.Decorate)) } diff --git a/internal/websocket/ws.go b/internal/websocket/ws.go index 7cc47bf..f6380aa 100644 --- a/internal/websocket/ws.go +++ b/internal/websocket/ws.go @@ -225,7 +225,7 @@ func (ws *Websocket) run(ctx context.Context) { Mode: mode.ToEvent(), } - // If auth fails, then continue with openfail xmidt connection + // If auth fails, then continue with no credentials. ws.credDecorator(ws.additionalHeaders) ws.conveyDecorator(ws.additionalHeaders)