generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint hostname parsing start filling out the local interface move files to admin package and fill out interface methods integration test integration test harness use loopback instead of hardcoding IPs better comment
- Loading branch information
Showing
10 changed files
with
258 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,76 @@ | ||
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" | ||
) | ||
|
||
type CmdClient 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) | ||
} | ||
|
||
// NewCmdClient 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 NewCmdClient(ctx context.Context, adminClient ftlv1connect.AdminServiceClient, endpoint *url.URL) (CmdClient, error) { | ||
_, err := adminClient.Ping(ctx, connect.NewRequest(&ftlv1.PingRequest{})) | ||
if isConnectUnavailableError(err) && isEndpointLocal(endpoint) { | ||
return newLocalCmdClient(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 { | ||
h := endpoint.Hostname() | ||
ips, err := net.LookupIP(h) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
for _, netip := range ips { | ||
if netip.IsLoopback() { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
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,45 @@ | ||
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) | ||
assert.Equal(t, isEndpointLocal(u), 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,59 @@ | ||
package admin | ||
|
||
import ( | ||
"context" | ||
|
||
"connectrpc.com/connect" | ||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
"github.com/TBD54566975/ftl/common/configuration" | ||
) | ||
|
||
// localCmdClient 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 localCmdClient struct { | ||
as *AdminService | ||
} | ||
|
||
func newLocalCmdClient(ctx context.Context) *localCmdClient { | ||
cm := configuration.ConfigFromContext(ctx) | ||
sm := configuration.SecretsFromContext(ctx) | ||
return &localCmdClient{NewAdminService(cm, sm)} | ||
} | ||
|
||
// Ping will always return a healthy response because localCmdClient is purely local. | ||
func (l *localCmdClient) Ping(ctx context.Context, req *connect.Request[ftlv1.PingRequest]) (*connect.Response[ftlv1.PingResponse], error) { | ||
return connect.NewResponse(&ftlv1.PingResponse{}), nil | ||
} | ||
|
||
func (l *localCmdClient) ConfigList(ctx context.Context, req *connect.Request[ftlv1.ListConfigRequest]) (*connect.Response[ftlv1.ListConfigResponse], error) { | ||
return l.as.ConfigList(ctx, req) | ||
} | ||
|
||
func (l *localCmdClient) ConfigGet(ctx context.Context, req *connect.Request[ftlv1.GetConfigRequest]) (*connect.Response[ftlv1.GetConfigResponse], error) { | ||
return l.as.ConfigGet(ctx, req) | ||
} | ||
|
||
func (l *localCmdClient) ConfigSet(ctx context.Context, req *connect.Request[ftlv1.SetConfigRequest]) (*connect.Response[ftlv1.SetConfigResponse], error) { | ||
return l.as.ConfigSet(ctx, req) | ||
} | ||
|
||
func (l *localCmdClient) ConfigUnset(ctx context.Context, req *connect.Request[ftlv1.UnsetConfigRequest]) (*connect.Response[ftlv1.UnsetConfigResponse], error) { | ||
return l.as.ConfigUnset(ctx, req) | ||
} | ||
|
||
func (l *localCmdClient) SecretsList(ctx context.Context, req *connect.Request[ftlv1.ListSecretsRequest]) (*connect.Response[ftlv1.ListSecretsResponse], error) { | ||
return l.as.SecretsList(ctx, req) | ||
} | ||
|
||
func (l *localCmdClient) SecretGet(ctx context.Context, req *connect.Request[ftlv1.GetSecretRequest]) (*connect.Response[ftlv1.GetSecretResponse], error) { | ||
return l.as.SecretGet(ctx, req) | ||
} | ||
|
||
func (l *localCmdClient) SecretSet(ctx context.Context, req *connect.Request[ftlv1.SetSecretRequest]) (*connect.Response[ftlv1.SetSecretResponse], error) { | ||
return l.as.SecretSet(ctx, req) | ||
} | ||
|
||
func (l *localCmdClient) SecretUnset(ctx context.Context, req *connect.Request[ftlv1.UnsetSecretRequest]) (*connect.Response[ftlv1.UnsetSecretResponse], error) { | ||
return l.as.SecretUnset(ctx, req) | ||
} |
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.