Files
nvke/cli/internal/auth/cache.go
Davanum SrinivasandClaude Fable 5.1 668bc38a9b feat(cli)!: rename nkectl to nvkectl, keeping the old names for one release (#1456)
The binary, its directory, its config directory and its environment
variables take the new name. For one release the previous names still
work: every NVKECTL_* setting is also read under its NKECTL_* name with a
deprecation warning (the NKECTL_CWE_* names of the generation before are
no longer read), a config file left under nkectl/ is read and copied to
nvkectl/ once, and releases publish both nvkectl-<os>-<arch> and
nkectl-<os>-<arch>, to GitHub and to S3 under the existing prefix.

The e2e helper package, the e2e test, the smoke and fetch scripts and
the bootstrap e2e workflow follow the binary; the fetch script accepts a
release that carries only the old asset name. The CLI's own documents
and the two installation documents that print the download URLs change
with it.

Kept on purpose: the NVCR repository name until an nvkectl repository
exists in the NGC org, the S3 prefix the publishing role's policy names,
and the prose in the other documents.

BREAKING CHANGE: the CLI is installed and invoked as nvkectl. The
previous binary name, environment variable names and config directory
are honoured for one release, as described above.

Closes #1455

Signed-off-by: Davanum Srinivas <dsrinivas@nvidia.com>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-10 17:04:03 -04:00

75 lines
1.5 KiB
Go

// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package auth
import (
"encoding/json"
"os"
"path/filepath"
"time"
)
type cachedToken struct {
Value string `json:"token"`
ExpiresAt time.Time `json:"expiresAt"`
}
func (t cachedToken) usableAt(now time.Time) bool {
return t.Value != "" && t.ExpiresAt.After(now.Add(tokenRenewBefore))
}
type tokenCache struct {
path string
tokens map[string]cachedToken
}
func loadTokenCache() *tokenCache {
cache := &tokenCache{tokens: make(map[string]cachedToken)}
dir, err := os.UserCacheDir()
if err != nil {
return cache
}
cache.path = filepath.Join(dir, "nvkectl", "tokens.json")
data, err := os.ReadFile(cache.path)
if err == nil {
var tokens map[string]cachedToken
if json.Unmarshal(data, &tokens) == nil && tokens != nil {
cache.tokens = tokens
}
}
return cache
}
func (c *tokenCache) store(endpoint string, token cachedToken) {
c.tokens[endpoint] = token
if c.path == "" {
return
}
if err := os.MkdirAll(filepath.Dir(c.path), 0o700); err != nil {
return
}
data, err := json.Marshal(c.tokens)
if err != nil {
return
}
temp, err := os.CreateTemp(filepath.Dir(c.path), ".tokens-*")
if err != nil {
return
}
tempPath := temp.Name()
defer os.Remove(tempPath)
if err := temp.Chmod(0o600); err != nil {
_ = temp.Close()
return
}
if _, err := temp.Write(data); err != nil {
_ = temp.Close()
return
}
if err := temp.Close(); err != nil {
return
}
_ = os.Rename(tempPath, c.path)
}