Skip to content

Agent Mesh

sam-node: share config completion with callers that have no file

Tomas Tormo67789b6f3463
diff --git a/internal/node/config.go b/internal/node/config.go
index 332a1b03..8d34ce87 100644
--- a/internal/node/config.go
+++ b/internal/node/config.go
@@ -57,10 +57,17 @@ func LoadNodeConfig(path string) (*NodeConfigComplete, error) {
path, config.Version, api.NodeConfigVersionV1Alpha1)
}
+ return CompleteNodeConfig(config)
+}
+
+// CompleteNodeConfig validates the services and labels and parses the Datalog
+// of a decoded node config. Shared with the mobile FFI so both report the same
+// errors.
+func CompleteNodeConfig(config api.NodeConfig) (*NodeConfigComplete, error) {
// The control plane attests this set at enrollment, so a malformed label
// must stop the node here rather than surface as a refused enrollment.
if err := api.ValidateLabels(config.Labels); err != nil {
- return nil, fmt.Errorf("invalid node config %s: %w", path, err)
+ return nil, fmt.Errorf("invalid labels: %w", err)
}
complete := &NodeConfigComplete{
diff --git a/internal/node/config_test.go b/internal/node/config_test.go
index ac54783c..a1cf687b 100644
--- a/internal/node/config_test.go
+++ b/internal/node/config_test.go
@@ -18,6 +18,8 @@ import (
"os"
"path/filepath"
"testing"
+
+ "github.com/google/sam/api"
)
func TestLoadNodeConfig(t *testing.T) {
@@ -315,3 +317,22 @@ labels:
})
}
}
+
+func TestCompleteNodeConfig(t *testing.T) {
+ got, err := CompleteNodeConfig(api.NodeConfig{
+ Services: []api.ServiceConfig{{Type: "mcp", Name: "phone-sensors"}},
+ Attenuation: api.Attenuation{Checks: []string{`check if label("region", "eu-west-1")`}},
+ })
+ if err != nil {
+ t.Fatalf("CompleteNodeConfig() error = %v", err)
+ }
+ if len(got.Checks) != 1 || len(got.Services) != 1 {
+ t.Fatalf("got %d checks and %d services, want 1 and 1", len(got.Checks), len(got.Services))
+ }
+
+ if _, err := CompleteNodeConfig(api.NodeConfig{
+ Attenuation: api.Attenuation{Checks: []string{"not datalog"}},
+ }); err == nil {
+ t.Fatal("CompleteNodeConfig() with invalid Datalog: want error, got nil")
+ }
+}