Skip to content

Commit 5f2acfb

Browse files
authored
[Repo Assist] refactor(auth): move generateRandomAPIKey to internal/auth package (#3163)
🤖 *This pull request was created by Repo Assist, an automated AI assistant.* ## Summary Moves `generateRandomAPIKey` from `internal/cmd/root.go` to `internal/auth/` as the exported `GenerateRandomAPIKey` function. API key generation is an authentication concern; the `internal/auth` package is the natural home. ## Changes - **`internal/auth/apikey.go`** — new file with `GenerateRandomAPIKey()` function - **`internal/auth/apikey_test.go`** — test migrated from `cmd/root_test.go` - **`internal/cmd/root.go`** — call site updated to `auth.GenerateRandomAPIKey()`; `crypto/rand` and `encoding/hex` imports removed - **`internal/cmd/root_test.go`** — `TestGenerateRandomAPIKey` removed (now lives in `auth` package) ## Motivation - Aligns the function's location with its semantic purpose (authentication) - Removes two low-level stdlib imports (`crypto/rand`, `encoding/hex`) from the CLI entry-point package - Makes `GenerateRandomAPIKey` reusable by any package that imports `internal/auth` ## Test Status ⚠️ **Infrastructure limitation**: The CI environment has Go 1.24.13 locally but `go.mod` requires Go 1.25.0, and the toolchain download is blocked by the network firewall. `make agent-finished` could not complete. The changes are syntactically verified by code review: - Correct import paths, correct exported name, existing test logic preserved verbatim - No functional changes to the gateway's behaviour Tests will run normally in the GitHub Actions CI environment where Go 1.25.0 is available. > Generated by [Repo Assist](https://github.com/github/gh-aw-mcpg/actions/runs/23978858416/agentic_workflow) · [◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+repo-assist%22&type=pullrequests) > > To install this [agentic workflow](https://github.com/githubnext/agentics/blob/851905c06e905bf362a9f6cc54f912e3df747d55/workflows/repo-assist.md), run > ``` > gh aw add githubnext/agentics@851905c > ``` <!-- gh-aw-agentic-workflow: Repo Assist, engine: copilot, model: auto, id: 23978858416, workflow_id: repo-assist, run: https://github.com/github/gh-aw-mcpg/actions/runs/23978858416 --> <!-- gh-aw-workflow-id: repo-assist -->
2 parents 6540158 + 60df958 commit 5f2acfb

4 files changed

Lines changed: 44 additions & 29 deletions

File tree

internal/auth/apikey.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package auth
2+
3+
import (
4+
"crypto/rand"
5+
"encoding/hex"
6+
"fmt"
7+
)
8+
9+
// GenerateRandomAPIKey generates a cryptographically random API key.
10+
// Per spec §7.3, the gateway SHOULD generate a random API key on startup
11+
// if none is provided. Returns a 32-byte hex-encoded string (64 chars).
12+
func GenerateRandomAPIKey() (string, error) {
13+
bytes := make([]byte, 32)
14+
if _, err := rand.Read(bytes); err != nil {
15+
return "", fmt.Errorf("failed to generate random API key: %w", err)
16+
}
17+
return hex.EncodeToString(bytes), nil
18+
}

internal/auth/apikey_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package auth_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/github/gh-aw-mcpg/internal/auth"
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
// TestGenerateRandomAPIKey verifies that GenerateRandomAPIKey produces a
12+
// non-empty, unique, hex-encoded string per spec §7.3.
13+
func TestGenerateRandomAPIKey(t *testing.T) {
14+
key, err := auth.GenerateRandomAPIKey()
15+
require.NoError(t, err, "GenerateRandomAPIKey() should not fail")
16+
assert.NotEmpty(t, key, "generated key should not be empty")
17+
// 32 bytes encoded as hex = 64 characters
18+
assert.Len(t, key, 64, "generated key should be 64 hex characters")
19+
20+
// Verify keys are unique across calls
21+
key2, err := auth.GenerateRandomAPIKey()
22+
require.NoError(t, err)
23+
assert.NotEqual(t, key, key2, "successive calls should produce unique keys")
24+
}

internal/cmd/root.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package cmd
33
import (
44
"bufio"
55
"context"
6-
"crypto/rand"
7-
"encoding/hex"
86
"encoding/json"
97
"fmt"
108
"io"
@@ -17,6 +15,7 @@ import (
1715
"syscall"
1816
"time"
1917

18+
"github.com/github/gh-aw-mcpg/internal/auth"
2019
"github.com/github/gh-aw-mcpg/internal/config"
2120
"github.com/github/gh-aw-mcpg/internal/difc"
2221
"github.com/github/gh-aw-mcpg/internal/logger"
@@ -304,7 +303,7 @@ func run(cmd *cobra.Command, args []string) error {
304303
// The generated key is set in the config so it propagates to both the HTTP
305304
// server authentication and the stdout configuration output (spec §5.4).
306305
if cfg.GetAPIKey() == "" {
307-
randomKey, err := generateRandomAPIKey()
306+
randomKey, err := auth.GenerateRandomAPIKey()
308307
if err != nil {
309308
return fmt.Errorf("failed to generate random API key: %w", err)
310309
}
@@ -650,17 +649,6 @@ func loadEnvFile(path string) error {
650649
return scanner.Err()
651650
}
652651

653-
// generateRandomAPIKey generates a cryptographically random API key.
654-
// Per spec §7.3, the gateway SHOULD generate a random API key on startup
655-
// if none is provided. Returns a 32-byte hex-encoded string (64 chars).
656-
func generateRandomAPIKey() (string, error) {
657-
bytes := make([]byte, 32)
658-
if _, err := rand.Read(bytes); err != nil {
659-
return "", fmt.Errorf("failed to generate random API key: %w", err)
660-
}
661-
return hex.EncodeToString(bytes), nil
662-
}
663-
664652
// Execute runs the root command
665653
func Execute() {
666654
if err := rootCmd.Execute(); err != nil {

internal/cmd/root_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -508,18 +508,3 @@ func TestPostRunCleanup(t *testing.T) {
508508
assert.NotNil(t, rootCmd.PersistentPostRun, "PersistentPostRun should be set")
509509
})
510510
}
511-
512-
// TestGenerateRandomAPIKey verifies that generateRandomAPIKey produces a
513-
// non-empty, unique, hex-encoded string per spec §7.3.
514-
func TestGenerateRandomAPIKey(t *testing.T) {
515-
key, err := generateRandomAPIKey()
516-
require.NoError(t, err, "generateRandomAPIKey() should not fail")
517-
assert.NotEmpty(t, key, "generated key should not be empty")
518-
// 32 bytes encoded as hex = 64 characters
519-
assert.Len(t, key, 64, "generated key should be 64 hex characters")
520-
521-
// Verify keys are unique across calls
522-
key2, err := generateRandomAPIKey()
523-
require.NoError(t, err)
524-
assert.NotEqual(t, key, key2, "successive calls should produce unique keys")
525-
}

0 commit comments

Comments
 (0)