Skip to content

Agent Mesh

feat: show the build version in the console and the Android app

Tomas Tormod0f66e486d42
diff --git a/internal/console/public/index.html b/internal/console/public/index.html
index aecb085a..82a1acb2 100644
--- a/internal/console/public/index.html
+++ b/internal/console/public/index.html
@@ -56,7 +56,10 @@
<aside class="sidebar">
<div class="sidebar-header">
<div class="logo"></div>
- <h2>SAM Console</h2>
+ <div>
+ <h2>SAM Console</h2>
+ <span class="version">__SAM_VERSION__</span>
+ </div>
</div>
<nav class="sidebar-nav" aria-label="Sections">
<a href="#overview" class="nav-item active" data-target="overview">
diff --git a/internal/console/public/style.css b/internal/console/public/style.css
index e3b4105d..0326f0fc 100644
--- a/internal/console/public/style.css
+++ b/internal/console/public/style.css
@@ -348,6 +348,12 @@ body {
letter-spacing: 0.5px;
}
+.sidebar-header .version {
+ display: block;
+ font-size: 0.75rem;
+ color: var(--text-secondary);
+}
+
.sidebar-nav {
padding: 20px 12px;
display: flex;
diff --git a/internal/console/server.go b/internal/console/server.go
index 39f0b3e7..9ac201c1 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -15,12 +15,14 @@
package console
import (
+ "bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
+ "html"
"io"
"io/fs"
"net/http"
@@ -33,6 +35,7 @@ import (
"github.com/coreos/go-oidc/v3/oidc"
"github.com/google/sam/api"
+ "github.com/google/sam/internal/version"
"golang.org/x/oauth2"
"google.golang.org/protobuf/proto"
)
@@ -219,9 +222,13 @@ func NewServer(cfg Config) (*Server, error) {
if name == "" {
name = "."
}
- if _, err := fs.Stat(assets, name); err != nil && r.URL.Path != "/" {
+ if name == "." || name == "index.html" {
+ serveIndex(w, r, assets)
+ return
+ }
+ if _, err := fs.Stat(assets, name); err != nil {
// SPA fallback: return index.html for unknown paths (useful for flutter/react router)
- http.ServeFileFS(w, r, assets, "index.html")
+ serveIndex(w, r, assets)
return
}
fileServer.ServeHTTP(w, r)
@@ -557,3 +564,18 @@ func generatePKCE() (string, string, error) {
challenge := base64.RawURLEncoding.EncodeToString(hash[:])
return verifier, challenge, nil
}
+
+// versionPlaceholder in index.html is replaced with the build version on every page load.
+const versionPlaceholder = "__SAM_VERSION__"
+
+// serveIndex reads index.html per request so a StaticDir stays live-editable.
+func serveIndex(w http.ResponseWriter, r *http.Request, assets fs.FS) {
+ page, err := fs.ReadFile(assets, "index.html")
+ if err != nil {
+ http.NotFound(w, r)
+ return
+ }
+ page = bytes.ReplaceAll(page, []byte(versionPlaceholder), []byte(html.EscapeString(version.String())))
+ w.Header().Set("Content-Type", "text/html; charset=utf-8")
+ _, _ = w.Write(page)
+}
diff --git a/internal/console/server_test.go b/internal/console/server_test.go
index f85de579..4a3fa239 100644
--- a/internal/console/server_test.go
+++ b/internal/console/server_test.go
@@ -32,6 +32,7 @@ import (
"github.com/coreos/go-oidc/v3/oidc"
"github.com/google/sam/api"
+ "github.com/google/sam/internal/version"
"google.golang.org/protobuf/proto"
)
@@ -670,3 +671,44 @@ func TestConsoleCSRFAndSecurityHeaders(t *testing.T) {
t.Errorf("same-origin POST /api/policies got %d, want 200", sameResp.StatusCode)
}
}
+
+// TestNewServer_StampsVersionIntoIndex: every path that serves index.html must carry the
+// build version, or the sidebar shows the raw placeholder.
+func TestNewServer_StampsVersionIntoIndex(t *testing.T) {
+ previous := version.Version
+ version.Version = "v9.9.9-test"
+ t.Cleanup(func() { version.Version = previous })
+
+ controlPlane := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK) // empty /info: no OIDC, which the console tolerates
+ }))
+ defer controlPlane.Close()
+
+ staticDir := t.TempDir()
+ if err := os.WriteFile(filepath.Join(staticDir, "index.html"), []byte("<span>"+versionPlaceholder+"</span>"), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ srv, err := NewServer(Config{
+ ControlPlaneURL: controlPlane.URL,
+ AdminToken: "test-admin-token",
+ StaticDir: staticDir,
+ })
+ if err != nil {
+ t.Fatalf("failed to create server: %v", err)
+ }
+ console := httptest.NewServer(srv.Handler())
+ defer console.Close()
+
+ client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
+ for _, path := range []string{"/", "/index.html", "/some/spa/route"} {
+ resp, err := client.Get(console.URL + path)
+ if err != nil {
+ t.Fatalf("GET %s: %v", path, err)
+ }
+ body, _ := io.ReadAll(resp.Body)
+ _ = resp.Body.Close()
+ if resp.StatusCode != http.StatusOK || string(body) != "<span>v9.9.9-test</span>" {
+ t.Errorf("GET %s: got %d %q, want 200 with the stamped version", path, resp.StatusCode, body)
+ }
+ }
+}
diff --git a/mobile/sam-node-app/lib/main.dart b/mobile/sam-node-app/lib/main.dart
index 8472c6c7..adbdde96 100644
--- a/mobile/sam-node-app/lib/main.dart
+++ b/mobile/sam-node-app/lib/main.dart
@@ -1505,6 +1505,12 @@ class _NodeControlPageState extends State<NodeControlPage> {
),
),
),
+ const SizedBox(height: 16),
+ Text(
+ 'SAM ${_samLib.getVersion()}',
+ textAlign: TextAlign.center,
+ style: Theme.of(context).textTheme.bodySmall,
+ ),
],
),
);
diff --git a/mobile/sam-node-app/lib/sam_ffi.dart b/mobile/sam-node-app/lib/sam_ffi.dart
index d53ae5f9..7f6f1a9e 100644
--- a/mobile/sam-node-app/lib/sam_ffi.dart
+++ b/mobile/sam-node-app/lib/sam_ffi.dart
@@ -57,6 +57,8 @@ typedef IsEnrolledDart = int Function(ffi.Pointer<Utf8> dataDir);
typedef GetMeshInfoC = ffi.Pointer<Utf8> Function();
typedef GetMeshInfoDart = ffi.Pointer<Utf8> Function();
+typedef GetVersionC = ffi.Pointer<Utf8> Function();
+typedef GetVersionDart = ffi.Pointer<Utf8> Function();
typedef FreeStringC = ffi.Void Function(ffi.Pointer<Utf8> str);
typedef FreeStringDart = void Function(ffi.Pointer<Utf8> str);
@@ -73,6 +75,7 @@ class SamNodeLib {
late FetchControlPlaneInfoJSONDart _fetchControlPlaneInfoJSON;
late IsEnrolledDart _isEnrolled;
late GetMeshInfoDart _getMeshInfo;
+ late GetVersionDart _getVersion;
late FreeStringDart _freeString;
SamNodeLib() {
@@ -94,6 +97,7 @@ class SamNodeLib {
_fetchControlPlaneInfoJSON = _dylib.lookupFunction<FetchControlPlaneInfoJSONC, FetchControlPlaneInfoJSONDart>('FetchControlPlaneInfoJSON');
_isEnrolled = _dylib.lookupFunction<IsEnrolledC, IsEnrolledDart>('IsEnrolled');
_getMeshInfo = _dylib.lookupFunction<GetMeshInfoC, GetMeshInfoDart>('GetMeshInfo');
+ _getVersion = _dylib.lookupFunction<GetVersionC, GetVersionDart>('GetVersion');
_freeString = _dylib.lookupFunction<FreeStringC, FreeStringDart>('FreeString');
}
@@ -125,6 +129,13 @@ class SamNodeLib {
return goID;
}
+ String getVersion() {
+ final cVersion = _getVersion();
+ final goVersion = cVersion.toDartString();
+ _freeString(cVersion);
+ return goVersion;
+ }
+
String? enroll(String dataDir, String controlPlaneURL, String jwt, bool allowLoopback, String labels, String refreshToken) {
final cDataDir = dataDir.toNativeUtf8();
final cControlPlaneURL = controlPlaneURL.toNativeUtf8();
diff --git a/mobile/sam-node-ffi/main.go b/mobile/sam-node-ffi/main.go
index b7e0e352..2b822e6b 100644
--- a/mobile/sam-node-ffi/main.go
+++ b/mobile/sam-node-ffi/main.go
@@ -21,6 +21,7 @@ import "C"
import (
"unsafe"
+ "github.com/google/sam/internal/version"
"github.com/google/sam/mobile/sam-node-ffi/ffi"
)
@@ -116,6 +117,11 @@ func GetMeshInfo() *C.char {
return C.CString(jsonStr)
}
+//export GetVersion
+func GetVersion() *C.char {
+ return C.CString(version.String())
+}
+
//export CallRemoteTool
func CallRemoteTool(peerID *C.char, toolName *C.char, argsJSON *C.char) *C.char {
goPeerID := C.GoString(peerID)