generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: re-enable
ftl config/secret
working without a running controll…
…er (#1695) Fixes #1677 Without starting a controller first, you can once again run `ftl config/secret` commands: ``` $ ftl config list key echo.default ```
- Loading branch information
Showing
10 changed files
with
228 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net" | ||
"net/url" | ||
|
||
"connectrpc.com/connect" | ||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect" | ||
) | ||
|
||
// Client standardizes an common interface between the AdminService as accessed via gRPC | ||
// and a purely-local variant that doesn't require a running controller to access. | ||
type Client interface { | ||
Ping(ctx context.Context, req *connect.Request[ftlv1.PingRequest]) (*connect.Response[ftlv1.PingResponse], error) | ||
|
||
// List configuration. | ||
ConfigList(ctx context.Context, req *connect.Request[ftlv1.ListConfigRequest]) (*connect.Response[ftlv1.ListConfigResponse], error) | ||
|
||
// Get a config value. | ||
ConfigGet(ctx context.Context, req *connect.Request[ftlv1.GetConfigRequest]) (*connect.Response[ftlv1.GetConfigResponse], error) | ||
|
||
// Set a config value. | ||
ConfigSet(ctx context.Context, req *connect.Request[ftlv1.SetConfigRequest]) (*connect.Response[ftlv1.SetConfigResponse], error) | ||
|
||
// Unset a config value. | ||
ConfigUnset(ctx context.Context, req *connect.Request[ftlv1.UnsetConfigRequest]) (*connect.Response[ftlv1.UnsetConfigResponse], error) | ||
|
||
// List secrets. | ||
SecretsList(ctx context.Context, req *connect.Request[ftlv1.ListSecretsRequest]) (*connect.Response[ftlv1.ListSecretsResponse], error) | ||
|
||
// Get a secret. | ||
SecretGet(ctx context.Context, req *connect.Request[ftlv1.GetSecretRequest]) (*connect.Response[ftlv1.GetSecretResponse], error) | ||
|
||
// Set a secret. | ||
SecretSet(ctx context.Context, req *connect.Request[ftlv1.SetSecretRequest]) (*connect.Response[ftlv1.SetSecretResponse], error) | ||
|
||
// Unset a secret. | ||
SecretUnset(ctx context.Context, req *connect.Request[ftlv1.UnsetSecretRequest]) (*connect.Response[ftlv1.UnsetSecretResponse], error) | ||
} | ||
|
||
// NewClient takes the service client and endpoint flag received by the cmd interface | ||
// and returns an appropriate interface for the cmd library to use. | ||
// | ||
// If the controller is not present AND endpoint is local, then inject a purely-local | ||
// implementation of the interface so that the user does not need to spin up a controller | ||
// just to run the `ftl config/secret` commands. Otherwise, return back the gRPC client. | ||
func NewClient(ctx context.Context, adminClient ftlv1connect.AdminServiceClient, endpoint *url.URL) (Client, error) { | ||
isLocal, err := isEndpointLocal(endpoint) | ||
if err != nil { | ||
return adminClient, err | ||
} | ||
_, err = adminClient.Ping(ctx, connect.NewRequest(&ftlv1.PingRequest{})) | ||
if isConnectUnavailableError(err) && isLocal { | ||
return newLocalClient(ctx), nil | ||
} | ||
return adminClient, nil | ||
} | ||
|
||
func isConnectUnavailableError(err error) bool { | ||
var connectErr *connect.Error | ||
if errors.As(err, &connectErr) { | ||
return connectErr.Code() == connect.CodeUnavailable | ||
} | ||
return false | ||
} | ||
|
||
func isEndpointLocal(endpoint *url.URL) (bool, error) { | ||
h := endpoint.Hostname() | ||
ips, err := net.LookupIP(h) | ||
if err != nil { | ||
return false, err | ||
} | ||
for _, netip := range ips { | ||
if netip.IsLoopback() { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package admin | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/alecthomas/assert/v2" | ||
) | ||
|
||
func TestIsEndpointLocal(t *testing.T) { | ||
tests := []struct { | ||
Name string | ||
Endpoint string | ||
Want bool | ||
}{ | ||
{ | ||
Name: "DefaultLocalhost", | ||
Endpoint: "http://localhost:8892", | ||
Want: true, | ||
}, | ||
{ | ||
Name: "NumericLocalhost", | ||
Endpoint: "http://127.0.0.1:8892", | ||
Want: true, | ||
}, | ||
{ | ||
Name: "TooLow", | ||
Endpoint: "http://126.255.255.255:8892", | ||
Want: false, | ||
}, | ||
{ | ||
Name: "TooHigh", | ||
Endpoint: "http://128.0.0.1:8892", | ||
Want: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.Name, func(t *testing.T) { | ||
u, err := url.Parse(test.Endpoint) | ||
assert.NoError(t, err) | ||
got, err := isEndpointLocal(u) | ||
assert.NoError(t, err) | ||
assert.Equal(t, got, test.Want) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/TBD54566975/ftl/common/configuration" | ||
) | ||
|
||
// localClient reads and writes to local projectconfig files without making any network | ||
// calls. It allows us to interface with local ftl-project.toml files without needing to | ||
// start a controller. | ||
type localClient struct { | ||
*AdminService | ||
} | ||
|
||
func newLocalClient(ctx context.Context) *localClient { | ||
cm := configuration.ConfigFromContext(ctx) | ||
sm := configuration.SecretsFromContext(ctx) | ||
return &localClient{NewAdminService(cm, sm)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[global] | ||
[global.configuration] | ||
key = "inline://InZhbHVlIg" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.