feat: expose build identity for release verification
This commit is contained in:
37
internal/buildinfo/buildinfo.go
Normal file
37
internal/buildinfo/buildinfo.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Package buildinfo 提供不包含凭据和业务数据的构建身份信息。
|
||||
package buildinfo
|
||||
|
||||
import "runtime"
|
||||
|
||||
// 这些变量由发布构建通过 -ldflags 注入;本地开发保持安全的默认值。
|
||||
var (
|
||||
Version = "dev"
|
||||
Revision = "unknown"
|
||||
BuiltAt = "unknown"
|
||||
)
|
||||
|
||||
// Info 是服务版本端点返回的固定结构,不包含主机、数据库或租户信息。
|
||||
type Info struct {
|
||||
Version string `json:"version"` // Version 发布版本或本地开发标识。
|
||||
Revision string `json:"revision"` // Revision 不可变源码提交标识。
|
||||
BuiltAt string `json:"built_at"` // BuiltAt 构建时间,由发布流水线提供。
|
||||
GoVersion string `json:"go_version"` // GoVersion 编译器版本。
|
||||
HTTPProtocolVersion string `json:"http_protocol_version"` // HTTPProtocolVersion HTTP接口主版本。
|
||||
WorkerProtocolVersion string `json:"worker_protocol_version"` // WorkerProtocolVersion Worker WebSocket子协议版本。
|
||||
EventProtocolVersion string `json:"event_protocol_version"` // EventProtocolVersion Event WebSocket子协议版本。
|
||||
WorkflowSchemaVersion int `json:"workflow_schema_version"` // WorkflowSchemaVersion Weave定义结构版本。
|
||||
}
|
||||
|
||||
// Current 返回当前进程的静态构建信息。
|
||||
func Current() Info {
|
||||
return Info{
|
||||
Version: Version,
|
||||
Revision: Revision,
|
||||
BuiltAt: BuiltAt,
|
||||
GoVersion: runtime.Version(),
|
||||
HTTPProtocolVersion: "1",
|
||||
WorkerProtocolVersion: "1",
|
||||
EventProtocolVersion: "1",
|
||||
WorkflowSchemaVersion: 1,
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.yuntuo.hk/k2safe/orbit/internal/buildinfo"
|
||||
"git.yuntuo.hk/k2safe/orbit/internal/observability"
|
||||
"git.yuntuo.hk/k2safe/orbit/pulse/service"
|
||||
)
|
||||
@@ -32,6 +33,13 @@ func New(addr string, ready Checker, tasks ...*service.Service) *http.Server {
|
||||
}
|
||||
write(w, http.StatusOK, map[string]string{"status": "ready", "scope": "database_and_schema"})
|
||||
})
|
||||
mux.HandleFunc("GET /version", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(buildinfo.Current())
|
||||
})
|
||||
if len(tasks) > 0 && tasks[0] != nil {
|
||||
registerTasks(mux, tasks[0])
|
||||
registerHistory(mux, tasks[0])
|
||||
|
||||
@@ -22,6 +22,15 @@ func TestHealthSeparatesLivenessAndReadiness(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionIsPublicAndBounded(t *testing.T) {
|
||||
server := New("127.0.0.1:0", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
server.Handler.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/version", nil))
|
||||
if rr.Code != http.StatusOK || !strings.Contains(rr.Body.String(), `"http_protocol_version":"1"`) || strings.Contains(rr.Body.String(), "ORBIT_MYSQL_DSN") {
|
||||
t.Fatalf("unexpected version response: %d %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMetricsListenerIsSeparateAndOptional(t *testing.T) {
|
||||
metrics := observability.NewRegistry()
|
||||
business := New("", nil)
|
||||
|
||||
Reference in New Issue
Block a user