Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,11 @@ var Commit = ""
func PrintableVersion() string {
return fmt.Sprintf("%s (%s)", Version, Commit)
}

// UserAgent returns the version-aware client identity used for outbound requests.
func UserAgent() string {
if Version == "" {
return "LocalAI"
}
return fmt.Sprintf("LocalAI/%s", Version)
}
56 changes: 56 additions & 0 deletions pkg/oci/cosignverify/useragent_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cosignverify

import (
"context"
"io"
"net/http"
"strings"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/mudler/LocalAI/internal"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

type roundTripFunc func(*http.Request) (*http.Response, error)

func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}

var _ = Describe("registry User-Agent", func() {
It("identifies LocalAI during signature verification requests", func() {
originalVersion := internal.Version
internal.Version = "v-test"
DeferCleanup(func() {
internal.Version = originalVersion
})

var userAgent string
transport := roundTripFunc(func(req *http.Request) (*http.Response, error) {
userAgent = req.Header.Get("User-Agent")
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{"application/vnd.oci.image.manifest.v1+json"},
"Docker-Content-Digest": []string{"sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
},
Body: io.NopCloser(strings.NewReader("")),
Request: req,
}, nil
})

verifier, err := NewVerifier(Policy{
Issuer: "https://token.actions.githubusercontent.com",
IdentityRegex: `^https://github\.com/example/.*`,
}, nil, transport)
Expect(err).NotTo(HaveOccurred())

ref, err := name.ParseReference("registry.example.com/localai/backend:latest")
Expect(err).NotTo(HaveOccurred())
_, err = remote.Head(ref, verifier.remoteOptions(context.Background())...)
Expect(err).NotTo(HaveOccurred())
Expect(userAgent).To(ContainSubstring("LocalAI/v-test"))
})
})
2 changes: 2 additions & 0 deletions pkg/oci/cosignverify/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"

"github.com/mudler/LocalAI/internal"
"github.com/sigstore/sigstore-go/pkg/root"
"github.com/sigstore/sigstore-go/pkg/tuf"
"github.com/sigstore/sigstore-go/pkg/verify"
Expand Down Expand Up @@ -297,6 +298,7 @@ func (v *Verifier) remoteOptions(ctx context.Context) []remote.Option {
opts := []remote.Option{
remote.WithContext(ctx),
remote.WithTransport(t),
remote.WithUserAgent(internal.UserAgent()),
}
if v.auth != nil {
opts = append(opts, remote.WithAuth(staticAuth{auth: v.auth}))
Expand Down
11 changes: 2 additions & 9 deletions pkg/oci/useragent.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package oci

import (
"fmt"

"github.com/mudler/LocalAI/internal"
)
import "github.com/mudler/LocalAI/internal"

// UserAgent returns the User-Agent string LocalAI sends on outbound registry
// requests (OCI registries and Ollama). It identifies the client as LocalAI
// and, when the binary was built with a version stamp, appends it so registries
// can attribute client-side usage to LocalAI rather than to the generic
// User-Agent of the underlying transport library.
func UserAgent() string {
if internal.Version == "" {
return "LocalAI"
}
return fmt.Sprintf("LocalAI/%s", internal.Version)
return internal.UserAgent()
}
Loading