diff --git a/README.md b/README.md index 22bad37..3f9278c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ func main() { | bazel | MODULE.bazel | | | bower | bower.json | | | brew | Brewfile | Brewfile.lock.json | +| buf | buf.yaml | buf.lock | | cargo | Cargo.toml | Cargo.lock | | carthage | Cartfile, Cartfile.private | Cartfile.resolved | | chef | metadata.rb, metadata.json, Berksfile | | @@ -88,6 +89,23 @@ func main() { | vagrant | Vagrantfile | | | vcpkg | vcpkg.json, ports/*/CONTROL | | +### Buf output + +For [Buf module configuration](https://buf.build/docs/configuration/v2/buf-yaml/#deps), +the parser reads only the external modules in `deps`. It does not report the +workspace's own modules, local paths, plugins or policies. A manifest preserves +a declared label or commit in `Version`; an unpinned dependency leaves it empty. +The [lockfile](https://buf.build/docs/configuration/v2/buf-lock/) supplies the +resolved commit and, when recorded, the opaque module digest in `Integrity`. +The v1/v1beta1 lock layout records the registry, owner and repository +separately; v2 records their combined module name. A lockfile without a +`version` uses the v1beta1 layout; a manifest requires a version. + +Modules have build scope. Manifest entries are direct; the lockfile does not +distinguish direct and transitive dependencies. Entries retain the +registry-qualified module name in `Name` and `buf` in `Ecosystem`. Their PURL +is empty because Buf has no registered PURL type. + ### Swift output Swift manifests are read without executing Swift, so variables, conditions, diff --git a/buf_test.go b/buf_test.go new file mode 100644 index 0000000..e54e987 --- /dev/null +++ b/buf_test.go @@ -0,0 +1,89 @@ +package manifests + +import ( + "errors" + "os" + "testing" +) + +func TestBufModules(t *testing.T) { + const commit = "9247f4104d234e2fa5f28ed89a1ee738" + const hash = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff" + type module struct{ name, ref, digest string } + for _, tc := range []struct { + file string + kind Kind + modules []module + }{ + {"v1/buf.yaml", Manifest, []module{{"buf.build/atelier/types", "", ""}, {"proto.example.org:8443/ledger/money", "stable", ""}}}, + {"v2/buf.yaml", Manifest, []module{{"buf.build/atelier/types", "", ""}, {"buf.build/atelier/money", commit, ""}}}, + {"v1/buf.lock", Lockfile, []module{{"proto.example.org:8443/ledger/money", commit, "shake256:" + hash}}}, + {"v2/buf.lock", Lockfile, []module{{"buf.build/atelier/money", commit, "b5:" + hash}}}, + } { + t.Run(tc.file, func(t *testing.T) { + path := "testdata/buf/" + tc.file + eco, kind, ok := Identify(path) + if !ok || eco != "buf" || kind != tc.kind { + t.Fatalf("Identify = (%q, %q, %v)", eco, kind, ok) + } + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + r, err := Parse(path, b) + if err != nil { + t.Fatal(err) + } + if r.Kind != tc.kind || r.Ecosystem != "buf" || len(r.Dependencies) != len(tc.modules) { + t.Fatalf("Parse = %+v", r) + } + for i, want := range tc.modules { + d := r.Dependencies[i] + if d.Name != want.name || d.Version != want.ref || d.Integrity != want.digest { + t.Errorf("module %d: %+v; want %+v", i, d, want) + } + if d.Ecosystem != "buf" || d.PURL != "" || d.NoPURL || d.Source != (Source{}) || d.Direct != (tc.kind == Manifest) || d.Scope != Build { + t.Errorf("module %d: unexpected dependency metadata: %+v", i, d) + } + } + }) + } +} + +func TestBufEmptyAndMalformed(t *testing.T) { + const legacyDeps = "deps: [{remote: buf.build, owner: acme, repository: types, commit: 9247f4104d234e2fa5f28ed89a1ee738}]\n" + for _, tc := range []struct { + file, input string + count int + invalid bool + }{ + {"buf.yaml", "version: v2\nmodules: [{path: proto}]\n", 0, false}, + {"buf.lock", "version: v1\ndeps: []\n", 0, false}, + {"buf.yaml", "version: v1beta1\ndeps: [buf.build/a/b]\n", 1, false}, + {"buf.lock", "version: v1beta1\n" + legacyDeps, 1, false}, + {"buf.lock", legacyDeps, 1, false}, + {"buf.lock", "version: v3\ndeps: []", 0, true}, + {"buf.yaml", "deps: [buf.build/a/b]\n", 0, true}, + {"buf.yaml", "version: v2\ndeps: [./proto, ../foo/bar, '', {}, 17, a/b/c/d, 'buf.build/a/b:', 'buf.build/a/b:x:y', buf.build/acme/ok]\n", 1, false}, + {"buf.lock", "version: v2\ndeps: [{name: buf.build/a/b}, null, {name: nonsense, commit: abc}, {name: buf.build/acme/ok, commit: 9247f4104d234e2fa5f28ed89a1ee738}]\n", 1, false}, + {"buf.lock", "version: v1\ndeps: [{remote: '', owner: acme, repository: types, commit: abc}]\n", 0, false}, + {"buf.yaml", "version: v2\ndeps: [", 0, true}, + {"buf.yaml", "version: v3\ndeps: []", 0, true}, + } { + r, err := Parse(tc.file, []byte(tc.input)) + if tc.invalid { + var parseErr *ParseError + if !errors.As(err, &parseErr) { + t.Errorf("%q: expected ParseError; got %v", tc.input, err) + } + continue + } + if err != nil { + t.Errorf("%q: %v", tc.input, err) + continue + } + if len(r.Dependencies) != tc.count { + t.Errorf("%q: got %+v", tc.input, r.Dependencies) + } + } +} diff --git a/imports.go b/imports.go index 83cf4c5..3c72b4a 100644 --- a/imports.go +++ b/imports.go @@ -8,6 +8,7 @@ import ( _ "github.com/git-pkgs/manifests/internal/asdf" _ "github.com/git-pkgs/manifests/internal/bazel" _ "github.com/git-pkgs/manifests/internal/brew" + _ "github.com/git-pkgs/manifests/internal/buf" _ "github.com/git-pkgs/manifests/internal/cargo" _ "github.com/git-pkgs/manifests/internal/carthage" _ "github.com/git-pkgs/manifests/internal/chef" diff --git a/internal/buf/buf.go b/internal/buf/buf.go new file mode 100644 index 0000000..714e0b6 --- /dev/null +++ b/internal/buf/buf.go @@ -0,0 +1,124 @@ +// Package buf extracts external Protobuf module dependencies from Buf configuration. +package buf + +import ( + "encoding/hex" + "fmt" + "net/url" + "strings" + "unicode" + + "github.com/git-pkgs/manifests/internal/core" + "go.yaml.in/yaml/v3" +) + +func init() { + core.Register("buf", core.Manifest, parser{kind: core.Manifest}, core.ExactMatch("buf.yaml")) + core.Register("buf", core.Lockfile, parser{kind: core.Lockfile}, core.ExactMatch("buf.lock")) +} + +type parser struct{ kind core.Kind } + +func (p parser) Parse(filename string, content []byte) (*core.Result, error) { + var doc struct { + Version string `yaml:"version"` + Deps []yaml.Node `yaml:"deps"` + } + if err := yaml.Unmarshal(content, &doc); err != nil { + return nil, &core.ParseError{Filename: filename, Err: err} + } + // Buf treats versionless lockfiles as v1beta1; manifests require a version. + if p.kind == core.Lockfile && doc.Version == "" { + doc.Version = "v1beta1" + } + switch doc.Version { + case "v1beta1", "v1", "v2": + default: + return nil, &core.ParseError{Filename: filename, Err: fmt.Errorf("unsupported Buf version %q", doc.Version)} + } + + result := &core.Result{} + for _, entry := range doc.Deps { + var name, ref, digest string + if p.kind == core.Manifest { + var raw string + if err := entry.Decode(&raw); err != nil { + continue + } + name, ref = manifestRef(raw) + } else { + name, ref, digest = lockPin(entry, doc.Version) + } + if name == "" { + continue + } + result.Dependencies = append(result.Dependencies, core.Dependency{ + Name: name, Version: ref, Integrity: digest, Scope: core.Build, + Direct: p.kind == core.Manifest, + }) + } + return result, nil +} + +// A reference follows the module, not the registry. The registry itself may +// include a port (registry.example:8443/owner/module:ref). +func manifestRef(s string) (name, ref string) { + registry, rest, ok := strings.Cut(s, "/") + if !ok { + return "", "" + } + module, ref, pinned := strings.Cut(rest, ":") + if pinned && (ref == "" || strings.Contains(ref, ":") || strings.IndexFunc(ref, unicode.IsSpace) >= 0) { + return "", "" + } + owner, repository, ok := strings.Cut(module, "/") + if !ok { + return "", "" + } + return moduleName(registry, owner, repository), ref +} + +func lockPin(entry yaml.Node, version string) (name, commit, digest string) { + var pin struct { + Name, Remote, Owner, Repository, Commit, Digest string + } + if err := entry.Decode(&pin); err != nil || !isCommitID(pin.Commit) { + return "", "", "" + } + if version == "v2" { + var ref string + name, ref = manifestRef(pin.Name) + if ref != "" { + return "", "", "" + } + } else { + name = moduleName(pin.Remote, pin.Owner, pin.Repository) + } + return name, pin.Commit, pin.Digest +} + +// Buf commit IDs are UUIDs written as 32 hex digits without dashes. +func isCommitID(s string) bool { + var id [16]byte + if len(s) != hex.EncodedLen(len(id)) { + return false + } + _, err := hex.Decode(id[:], []byte(s)) + return err == nil +} + +func moduleName(registry, owner, repository string) string { + if registry == "" || registry == "." || registry == ".." || strings.ContainsAny(registry, "/?#@\\") { + return "" + } + u, err := url.Parse("//" + registry) + if err != nil || u.Hostname() == "" || u.Host != registry { + return "" + } + for _, part := range []string{owner, repository} { + if part == "" || part == "." || part == ".." || strings.ContainsAny(part, "/:?#@\\") || strings.IndexFunc(part, unicode.IsSpace) >= 0 { + return "" + } + } + return registry + "/" + owner + "/" + repository +} diff --git a/manifests.go b/manifests.go index 0316c11..a5ba556 100644 --- a/manifests.go +++ b/manifests.go @@ -284,10 +284,11 @@ func declarationPURL(ecosystem string, declaration core.Declaration) string { // makePURL creates a Package URL for a dependency. func makePURL(ecosystem, name, version, registryURL string) string { - // Chef and Vagrant are still candidate PURL types without accepted name or - // namespace rules. Keep identities empty instead of inventing mappings that - // callers could mistake for standardized PURLs. - if ecosystem == "chef" || ecosystem == "vagrant" { + // Chef and Vagrant are candidate PURL types without accepted name or + // namespace rules, and Buf has no registered PURL type. Leave PURLs empty + // rather than inventing mappings for those ecosystems. + switch ecosystem { + case "chef", "vagrant", "buf": return "" } return purl.BuildPURLString(ecosystem, name, version, registryURL) diff --git a/manifests_test.go b/manifests_test.go index c08e888..c5d6eca 100644 --- a/manifests_test.go +++ b/manifests_test.go @@ -662,6 +662,12 @@ func TestIdentifyFiles(t *testing.T) { {"apm.lock.yaml", "apm", Lockfile, true}, {"apm.lock", "apm", Lockfile, true}, + // buf + {"buf.yaml", "buf", Manifest, true}, + {"buf.lock", "buf", Lockfile, true}, + {"buf.work.yaml", "", "", false}, + {"buf.gen.yaml", "", "", false}, + // npm {"package.json", "npm", Manifest, true}, {"package-lock.json", "npm", Lockfile, true}, diff --git a/testdata/buf/v1/buf.lock b/testdata/buf/v1/buf.lock new file mode 100644 index 0000000..69f7f9b --- /dev/null +++ b/testdata/buf/v1/buf.lock @@ -0,0 +1,7 @@ +version: v1 +deps: + - remote: proto.example.org:8443 + owner: ledger + repository: money + commit: 9247f4104d234e2fa5f28ed89a1ee738 + digest: shake256:00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff diff --git a/testdata/buf/v1/buf.yaml b/testdata/buf/v1/buf.yaml new file mode 100644 index 0000000..b5f16a9 --- /dev/null +++ b/testdata/buf/v1/buf.yaml @@ -0,0 +1,5 @@ +version: v1 +name: buf.build/atelier/invoice +deps: + - buf.build/atelier/types + - proto.example.org:8443/ledger/money:stable diff --git a/testdata/buf/v2/buf.lock b/testdata/buf/v2/buf.lock new file mode 100644 index 0000000..3e4de69 --- /dev/null +++ b/testdata/buf/v2/buf.lock @@ -0,0 +1,5 @@ +version: v2 +deps: + - name: buf.build/atelier/money + commit: 9247f4104d234e2fa5f28ed89a1ee738 + digest: b5:00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff diff --git a/testdata/buf/v2/buf.yaml b/testdata/buf/v2/buf.yaml new file mode 100644 index 0000000..c90b574 --- /dev/null +++ b/testdata/buf/v2/buf.yaml @@ -0,0 +1,8 @@ +version: v2 +modules: + - path: proto/invoice + name: buf.build/atelier/invoice + - path: proto/stock +deps: + - buf.build/atelier/types + - buf.build/atelier/money:9247f4104d234e2fa5f28ed89a1ee738