Files
Privatae e4c9b69b46
ci / personal-data (push) Successful in 5s
ci / test (push) Successful in 3m4s
ci / fuzz (push) Successful in 3m45s
bastion 0.53.2
2026-10-20 04:42:14 +00:00

61 lines
1.5 KiB
Go

package main
import (
"errors"
"fmt"
"go/ast"
"go/parser"
"go/token"
"strconv"
)
// PinnedKeys reads the productionReleaseKeys list out of a bastion source
// file (client-go/release_sign.go) by parsing it as Go, so the publish and
// verify scripts see exactly the keys the compiler does: comments are not
// keys, and the list's layout does not matter.
func PinnedKeys(path string) ([]string, error) {
f, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.SkipObjectResolution)
if err != nil {
return nil, err
}
var found *ast.CompositeLit
for _, d := range f.Decls {
g, ok := d.(*ast.GenDecl)
if !ok || g.Tok != token.VAR {
continue
}
for _, sp := range g.Specs {
vs, ok := sp.(*ast.ValueSpec)
if !ok {
continue
}
for i, n := range vs.Names {
if n.Name != "productionReleaseKeys" || i >= len(vs.Values) {
continue
}
lit, ok := vs.Values[i].(*ast.CompositeLit)
if !ok {
return nil, errors.New("productionReleaseKeys is not a list literal")
}
found = lit
}
}
}
if found == nil {
return nil, fmt.Errorf("%s declares no productionReleaseKeys", path)
}
var keys []string
for _, e := range found.Elts {
b, ok := e.(*ast.BasicLit)
if !ok || b.Kind != token.STRING {
return nil, errors.New("productionReleaseKeys holds something other than string literals")
}
k, err := strconv.Unquote(b.Value)
if err != nil {
return nil, err
}
keys = append(keys, k)
}
return keys, nil
}