Skip to content

Commit eb63961

Browse files
committed
comments
Signed-off-by: Grant Linville <[email protected]>
1 parent ad32ae5 commit eb63961

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

pkg/credentials/dbstore.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ type DBStore struct {
4444

4545
// GptscriptCredential is the struct we use to represent credentials in the database.
4646
type GptscriptCredential struct {
47-
ID uint `gorm:"primarykey"`
48-
CreatedAt time.Time
49-
UpdatedAt time.Time
50-
Context string `gorm:"index:contextname,unique"`
51-
Name string `gorm:"index:contextname,unique"`
47+
// We aren't using gorm.Model because we don't want a DeletedAt field.
48+
// We want records to be fully deleted from the database.
49+
ID uint `gorm:"primarykey"`
50+
CreatedAt time.Time
51+
UpdatedAt time.Time
52+
53+
// We set up an extra index here to enforce a unique constraint on context+name.
54+
Context string `gorm:"index:contextname,unique"`
55+
Name string `gorm:"index:contextname,unique"`
56+
5257
Type, Env, RefreshToken string
5358
Ephemeral bool
5459
ExpiresAt *time.Time
@@ -98,6 +103,7 @@ func NewDBStore(ctx context.Context, cfg *config.CLIConfig, credCtxs []string) (
98103
if err != nil {
99104
return nil, fmt.Errorf("failed to read encryption config: %w", err)
100105
} else if encryptionConf != nil {
106+
// The transformer that we get from the encryption configuration is the interface we use for encryption and decryption.
101107
transformer, exists := encryptionConf.Transformers[groupResource]
102108
if !exists {
103109
return nil, fmt.Errorf("failed to find encryption transformer for %s", groupResource.String())
@@ -150,6 +156,7 @@ func readEncryptionConfig(ctx context.Context) (*encryptionconfig.EncryptionConf
150156
return nil, fmt.Errorf("failed to stat encryption config file: %w", err)
151157
}
152158

159+
// Use k8s libraries to load the encryption config from the file:
153160
return encryptionconfig.LoadEncryptionConfig(ctx, encryptionConfigPath, false, "gptscript")
154161
}
155162

pkg/credentials/dbstore_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,61 @@ package credentials
22

33
import (
44
"context"
5+
"crypto/rand"
6+
"fmt"
57
"testing"
68

79
"github.com/gptscript-ai/gptscript/pkg/config"
810
"github.com/stretchr/testify/require"
911
)
1012

1113
func TestDBStore(t *testing.T) {
12-
const credCtx = "default"
14+
const credCtx = "testing"
15+
16+
bytes := make([]byte, 16)
17+
_, err := rand.Read(bytes)
18+
require.NoError(t, err)
19+
1320
credential := Credential{
1421
Context: credCtx,
15-
ToolName: "mytestcred",
22+
ToolName: fmt.Sprintf("%x", bytes),
1623
Type: CredentialTypeTool,
17-
Env: map[string]string{"ASDF": "yeet"},
24+
Env: map[string]string{"ENV_VAR": "value"},
1825
RefreshToken: "myrefreshtoken",
1926
}
2027

2128
cfg, _ := config.ReadCLIConfig("")
2229

30+
// Set up the store
2331
store, err := NewDBStore(context.Background(), cfg, []string{credCtx})
2432
require.NoError(t, err)
2533

34+
// Create the credential
2635
require.NoError(t, store.Add(context.Background(), credential))
2736

37+
// Get the credential
2838
cred, found, err := store.Get(context.Background(), credential.ToolName)
2939
require.NoError(t, err)
3040
require.True(t, found)
3141
require.Equal(t, credential.Env, cred.Env)
3242
require.Equal(t, credential.RefreshToken, cred.RefreshToken)
3343

44+
// List credentials and check for it
3445
list, err := store.List(context.Background())
3546
require.NoError(t, err)
3647
require.Greater(t, len(list), 0)
48+
49+
found = false
3750
for _, c := range list {
3851
if c.Context == credCtx && c.ToolName == credential.ToolName {
3952
require.Equal(t, credential.Env, c.Env)
4053
require.Equal(t, credential.RefreshToken, c.RefreshToken)
54+
found = true
55+
break
4156
}
4257
}
58+
require.True(t, found)
4359

60+
// Delete the credential
4461
require.NoError(t, store.Remove(context.Background(), credential.ToolName))
4562
}

0 commit comments

Comments
 (0)