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
11 changes: 10 additions & 1 deletion malwaredb/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"common/settings"
"common/token"
"net/url"
"os"
"strings"
"time"
)

// LicenseHeader is the X-Nvm-License value for feed GETs.
// Certified builds may send a real AccessToken via mirrorauth.
// Community builds mint a short-lived temporary token.
// Pack/CI can inject NVM_ACCESS_TOKEN or NVM_LICENSE for seed fetches.
// Community runtime falls back to a short-lived temporary token.
func LicenseHeader() string {
head, err := url.Parse(DefaultFeedBase + "/v1/head.cose")
if err == nil {
Expand All @@ -24,6 +26,13 @@ func LicenseHeader() string {
}
}
}
// Pack/CI seed fetches set NVM_ACCESS_TOKEN before any installed AccessToken.
if env := strings.TrimSpace(os.Getenv("NVM_ACCESS_TOKEN")); env != "" {
return strings.TrimPrefix(env, "Bearer ")
}
if env := strings.TrimSpace(os.Getenv("NVM_LICENSE")); env != "" {
return strings.TrimPrefix(env, "Bearer ")
}
access := strings.TrimSpace(settings.Global().AccessToken)
access = strings.TrimPrefix(access, "Bearer ")
if access != "" {
Expand Down
30 changes: 30 additions & 0 deletions malwaredb/license_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package malwaredb

import (
"os"
"testing"
)

func TestLicenseHeaderPrefersEnvToken(t *testing.T) {
t.Setenv("NVM_ACCESS_TOKEN", "")
t.Setenv("NVM_LICENSE", "")
prev := LicenseHeader()
if prev == "" {
t.Fatal("LicenseHeader() empty without env; want temporary token")
}

t.Setenv("NVM_ACCESS_TOKEN", "Bearer pack-time-token")
got := LicenseHeader()
if got != "pack-time-token" {
t.Fatalf("LicenseHeader() = %q, want pack-time-token", got)
}

t.Setenv("NVM_ACCESS_TOKEN", "")
t.Setenv("NVM_LICENSE", "env-license")
got = LicenseHeader()
if got != "env-license" {
t.Fatalf("LicenseHeader() = %q, want env-license", got)
}

_ = os.Unsetenv("NVM_LICENSE")
}
Loading