Skip to content

Modern Docker Management, Designed for Everyone

refactor: move crypto logic into go.getarcane.app/sys/crypto (#3164)

Kyle Mendell687754360648
diff --git a/backend/api/handlers/container_registries.go b/backend/api/handlers/container_registries.go
index d32312d1a..6a6c31a49 100644
--- a/backend/api/handlers/container_registries.go
+++ b/backend/api/handlers/container_registries.go
@@ -11,10 +11,10 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
"github.com/getarcaneapp/arcane/backend/v2/internal/services"
"github.com/getarcaneapp/arcane/backend/v2/pkg/authz"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/mapper"
"github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
+ "go.getarcane.app/sys/crypto"
)
// ContainerRegistryHandler handles container registry management endpoints.
diff --git a/backend/api/handlers/edge_mtls_ca_test.go b/backend/api/handlers/edge_mtls_ca_test.go
index 841933f4d..9ffcabbf6 100644
--- a/backend/api/handlers/edge_mtls_ca_test.go
+++ b/backend/api/handlers/edge_mtls_ca_test.go
@@ -8,9 +8,9 @@ import (
"time"
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
- libcrypto "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge"
"github.com/stretchr/testify/require"
+ libcrypto "go.getarcane.app/sys/crypto"
)
func TestMain(m *testing.M) {
diff --git a/backend/api/handlers/notifications_test.go b/backend/api/handlers/notifications_test.go
index 0313d9622..77cff7a97 100644
--- a/backend/api/handlers/notifications_test.go
+++ b/backend/api/handlers/notifications_test.go
@@ -11,11 +11,11 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
"github.com/getarcaneapp/arcane/backend/v2/internal/services"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
notificationdto "github.com/getarcaneapp/arcane/types/v2/notification"
glsqlite "github.com/glebarez/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "go.getarcane.app/sys/crypto"
"gorm.io/gorm"
)
diff --git a/backend/go.mod b/backend/go.mod
index a249bbaa1..8b7734ebf 100644
--- a/backend/go.mod
+++ b/backend/go.mod
@@ -57,6 +57,7 @@ require (
go.getarcane.app/docker/convert v0.1.0
go.getarcane.app/sys/atomic v0.1.0
go.getarcane.app/sys/cgroup v0.1.0
+ go.getarcane.app/sys/crypto v0.1.0
go.getarcane.app/updater v0.5.0
go.yaml.in/yaml/v4 v4.0.0-rc.6
golang.org/x/crypto v0.53.0
diff --git a/backend/go.sum b/backend/go.sum
index aefd06c77..193319273 100644
--- a/backend/go.sum
+++ b/backend/go.sum
@@ -543,6 +543,8 @@ go.getarcane.app/sys/atomic v0.1.0 h1:0HhUeGXYdKAmMW24O+OaqcjDZx6twAuYPBu9T91zKs
go.getarcane.app/sys/atomic v0.1.0/go.mod h1:QPb5eUXYKYG5acp9jJBAZ/PTbHbByDAQG6eDNAsZZ2w=
go.getarcane.app/sys/cgroup v0.1.0 h1:SCAbuU+rZ/EjMU8kkVxmXZsMchiZp9eFdMQFnbU+asA=
go.getarcane.app/sys/cgroup v0.1.0/go.mod h1:Paj4QOazrTXO/Ivhmvt1E/RjPJFTdKkxRgRDk4qBCUI=
+go.getarcane.app/sys/crypto v0.1.0 h1:HDQ2CKHkm+wZe5xb/qR7MzW5fKi0Wq8Jcyr7qDVgRWg=
+go.getarcane.app/sys/crypto v0.1.0/go.mod h1:YtoRB0TENwfdNl0ZUTWkeas/8rT7NhCbI8ENsZzCCdU=
go.getarcane.app/updater v0.5.0 h1:H7CtjrFWb7CbOBHq0sqRsmGlGr+MMh4Lt5/pIpgQWec=
go.getarcane.app/updater v0.5.0/go.mod h1:8f0/0pi7J0yFqgwFTywiwk/NoUN/g6AfmprOuAUrI20=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.go
index 0403549a0..ce34168b9 100644
--- a/backend/internal/bootstrap/bootstrap.go
+++ b/backend/internal/bootstrap/bootstrap.go
@@ -20,7 +20,6 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/di"
"github.com/getarcaneapp/arcane/backend/v2/internal/services"
- libcrypto "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge"
tunnelpb "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge/proto/tunnel/v1"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/logstream"
@@ -28,6 +27,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/pkg/scheduler"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
httputils "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/httpx"
+ libcrypto "go.getarcane.app/sys/crypto"
"google.golang.org/grpc"
)
@@ -113,6 +113,21 @@ func Bootstrap(ctx context.Context) error {
return nil
}
+// isWeakProductionEncryptionKeyInternal reports whether an explicit
+// ENCRYPTION_KEY is an unprefixed passphrase shorter than 32 characters in
+// production. libcrypto derives a key from any non-empty passphrase, so this
+// preserves the historical fail-fast rejection of low-entropy production keys.
+func isWeakProductionEncryptionKeyInternal(encryptionKey, environment string, agentMode bool) bool {
+ if environment != "production" || agentMode {
+ return false
+ }
+ key := strings.TrimSpace(encryptionKey)
+ if key == "" || strings.HasPrefix(key, "hex:") || strings.HasPrefix(key, "base64:") {
+ return false
+ }
+ return len(strings.TrimPrefix(key, "raw:")) < 32
+}
+
func newConfiguredHTTPClient(cfg *config.Config) *http.Client {
if cfg.HTTPClientTimeout > 0 {
return httputils.NewHTTPClientWithTimeout(time.Duration(cfg.HTTPClientTimeout) * time.Second)
@@ -139,6 +154,10 @@ func initializeStartupState(appCtx context.Context, cfg *config.Config, appServi
cfg.AgentToken = runtimeCfg.AgentToken
cfg.EncryptionKey = runtimeCfg.EncryptionKey
+ if isWeakProductionEncryptionKeyInternal(cfg.EncryptionKey, string(cfg.Environment), cfg.AgentMode) {
+ panic("ENCRYPTION_KEY passphrase must be at least 32 characters in production (or use a hex:/base64: encoded 32-byte key)")
+ }
+
libcrypto.InitEncryption(&libcrypto.Config{
EncryptionKey: cfg.EncryptionKey,
Environment: string(cfg.Environment),
diff --git a/backend/internal/bootstrap/bootstrap_test.go b/backend/internal/bootstrap/bootstrap_test.go
index 159fe3bcb..6ee6dc155 100644
--- a/backend/internal/bootstrap/bootstrap_test.go
+++ b/backend/internal/bootstrap/bootstrap_test.go
@@ -14,10 +14,10 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/di"
"github.com/getarcaneapp/arcane/backend/v2/internal/middleware"
- libcrypto "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
tunnelpb "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge/proto/tunnel/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ libcrypto "go.getarcane.app/sys/crypto"
"golang.org/x/net/http2"
)
@@ -294,3 +294,10 @@ func TestPrepareServerTLSInternal_AllowsExternalMTLSTermination(t *testing.T) {
require.FileExists(t, assetsDir+"/ca.crt")
require.FileExists(t, assetsDir+"/ca.key")
}
+
+func TestIsWeakProductionEncryptionKeyInternal(t *testing.T) {
+ assert.True(t, isWeakProductionEncryptionKeyInternal("short", "production", false))
+ assert.False(t, isWeakProductionEncryptionKeyInternal("test-encryption-key-for-edge-mtls-32bytes-min", "production", false))
+ assert.False(t, isWeakProductionEncryptionKeyInternal("hex:abc", "production", false))
+ assert.False(t, isWeakProductionEncryptionKeyInternal("short", "development", false))
+}
diff --git a/backend/internal/services/build_service_test.go b/backend/internal/services/build_service_test.go
index 8d780e608..d2a88be2f 100644
--- a/backend/internal/services/build_service_test.go
+++ b/backend/internal/services/build_service_test.go
@@ -13,11 +13,11 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
buildgit "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
glsqlite "github.com/glebarez/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
buildtypes "go.getarcane.app/builds/types"
+ "go.getarcane.app/sys/crypto"
"gorm.io/gorm"
)
diff --git a/backend/internal/services/container_registry_service.go b/backend/internal/services/container_registry_service.go
index 1d935a9a7..69101e965 100644
--- a/backend/internal/services/container_registry_service.go
+++ b/backend/internal/services/container_registry_service.go
@@ -17,7 +17,6 @@ import (
cerrdefs "github.com/containerd/errdefs"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
utilsregistry "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/registryauth"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
@@ -25,6 +24,7 @@ import (
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
dockerregistry "github.com/moby/moby/api/types/registry"
"github.com/moby/moby/client"
+ "go.getarcane.app/sys/crypto"
updaterdigest "go.getarcane.app/updater/pkg/digest"
updaterrefs "go.getarcane.app/updater/pkg/refs"
updaterregistry "go.getarcane.app/updater/pkg/registry"
diff --git a/backend/internal/services/container_registry_service_test.go b/backend/internal/services/container_registry_service_test.go
index b58e8124d..6a793549a 100644
--- a/backend/internal/services/container_registry_service_test.go
+++ b/backend/internal/services/container_registry_service_test.go
@@ -11,7 +11,6 @@ import (
"testing"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
dockerregistry "github.com/moby/moby/api/types/registry"
"github.com/moby/moby/client"
@@ -19,6 +18,7 @@ import (
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "go.getarcane.app/sys/crypto"
)
type fakeRegistryDaemonClient struct {
diff --git a/backend/internal/services/ecr_token_service.go b/backend/internal/services/ecr_token_service.go
index 72e40ac5e..22d370973 100644
--- a/backend/internal/services/ecr_token_service.go
+++ b/backend/internal/services/ecr_token_service.go
@@ -13,7 +13,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ecr"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
+ "go.getarcane.app/sys/crypto"
)
const ecrTokenTTL = 12 * time.Hour
diff --git a/backend/internal/services/environment_service.go b/backend/internal/services/environment_service.go
index f4c913a88..78a9067ec 100644
--- a/backend/internal/services/environment_service.go
+++ b/backend/internal/services/environment_service.go
@@ -14,7 +14,6 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/timeouts"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
@@ -27,6 +26,7 @@ import (
schedulertypes "github.com/getarcaneapp/arcane/types/v2/scheduler"
"github.com/google/uuid"
"github.com/moby/moby/client"
+ "go.getarcane.app/sys/crypto"
"gorm.io/gorm"
)
diff --git a/backend/internal/services/environment_service_test.go b/backend/internal/services/environment_service_test.go
index 77eef8ff5..4d209b8bb 100644
--- a/backend/internal/services/environment_service_test.go
+++ b/backend/internal/services/environment_service_test.go
@@ -20,13 +20,13 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/edge"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
"github.com/getarcaneapp/arcane/types/v2/environment"
"github.com/getarcaneapp/arcane/types/v2/gitops"
"github.com/gorilla/websocket"
+ "go.getarcane.app/sys/crypto"
)
func setupEnvironmentServiceTestDB(t *testing.T) *database.DB {
diff --git a/backend/internal/services/git_repository_service.go b/backend/internal/services/git_repository_service.go
index fa9b28ce8..80173e9b5 100644
--- a/backend/internal/services/git_repository_service.go
+++ b/backend/internal/services/git_repository_service.go
@@ -9,12 +9,12 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
git "github.com/getarcaneapp/arcane/backend/v2/pkg/gitutil"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/timeouts"
"github.com/getarcaneapp/arcane/backend/v2/pkg/pagination"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/types/v2/gitops"
contextsource "go.getarcane.app/builds/pkg/utils/contextsource"
+ "go.getarcane.app/sys/crypto"
"gorm.io/gorm"
)
diff --git a/backend/internal/services/git_repository_service_test.go b/backend/internal/services/git_repository_service_test.go
index 7bae7a244..58c5a5e87 100644
--- a/backend/internal/services/git_repository_service_test.go
+++ b/backend/internal/services/git_repository_service_test.go
@@ -14,7 +14,7 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
+ "go.getarcane.app/sys/crypto"
)
func setupGitRepositoryServiceTestInternal(t *testing.T) (*GitRepositoryService, *database.DB) {
diff --git a/backend/internal/services/image_service_test.go b/backend/internal/services/image_service_test.go
index d53f95d45..b8afa9e65 100644
--- a/backend/internal/services/image_service_test.go
+++ b/backend/internal/services/image_service_test.go
@@ -19,7 +19,6 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/types/v2/containerregistry"
imagetypes "github.com/getarcaneapp/arcane/types/v2/image"
"github.com/getarcaneapp/arcane/types/v2/vulnerability"
@@ -27,6 +26,7 @@ import (
dockerregistry "github.com/moby/moby/api/types/registry"
"github.com/moby/moby/client"
"github.com/stretchr/testify/assert"
+ "go.getarcane.app/sys/crypto"
)
func TestGetImageIDsFromSummariesInternal(t *testing.T) {
diff --git a/backend/internal/services/image_update_service.go b/backend/internal/services/image_update_service.go
index 9753051fc..bfb6eecda 100644
--- a/backend/internal/services/image_update_service.go
+++ b/backend/internal/services/image_update_service.go
@@ -14,7 +14,6 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
activitylib "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/activity"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/ratelimit"
registry "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/registryauth"
"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/timeouts"
@@ -23,6 +22,7 @@ import (
"github.com/getarcaneapp/arcane/types/v2/imageupdate"
"github.com/moby/moby/api/types/image"
"github.com/moby/moby/client"
+ "go.getarcane.app/sys/crypto"
updaterdigest "go.getarcane.app/updater/pkg/digest"
"golang.org/x/sync/errgroup"
"gorm.io/gorm"
diff --git a/backend/internal/services/notification_service.go b/backend/internal/services/notification_service.go
index 4a63207ff..46aeb735e 100644
--- a/backend/internal/services/notification_service.go
+++ b/backend/internal/services/notification_service.go
@@ -19,12 +19,12 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/notifications"
"github.com/getarcaneapp/arcane/backend/v2/resources"
"github.com/getarcaneapp/arcane/types/v2/imageupdate"
notificationdto "github.com/getarcaneapp/arcane/types/v2/notification"
"github.com/getarcaneapp/arcane/types/v2/system"
+ "go.getarcane.app/sys/crypto"
)
var notificationCredentialFieldsByProviderInternal = map[models.NotificationProvider][]string{
diff --git a/backend/internal/services/notification_service_test.go b/backend/internal/services/notification_service_test.go
index 6890aac80..d40301aee 100644
--- a/backend/internal/services/notification_service_test.go
+++ b/backend/internal/services/notification_service_test.go
@@ -21,11 +21,11 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/notifications"
"github.com/getarcaneapp/arcane/types/v2/imageupdate"
notificationdto "github.com/getarcaneapp/arcane/types/v2/notification"
"github.com/getarcaneapp/arcane/types/v2/system"
+ "go.getarcane.app/sys/crypto"
)
func setupNotificationTestDB(t *testing.T) *database.DB {
diff --git a/backend/internal/services/updater_service_test.go b/backend/internal/services/updater_service_test.go
index 455b9b024..0c69353f4 100644
--- a/backend/internal/services/updater_service_test.go
+++ b/backend/internal/services/updater_service_test.go
@@ -16,13 +16,13 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/config"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
arcaneupdater "github.com/getarcaneapp/arcane/types/v2/updater"
"github.com/moby/moby/api/types/container"
dockertypesimage "github.com/moby/moby/api/types/image"
"github.com/moby/moby/client"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "go.getarcane.app/sys/crypto"
moduleapi "go.getarcane.app/updater/api"
updaterlabels "go.getarcane.app/updater/pkg/labels"
moduletypes "go.getarcane.app/updater/types"
diff --git a/backend/internal/services/webhook_service.go b/backend/internal/services/webhook_service.go
index ddc52f572..8a066f0c2 100644
--- a/backend/internal/services/webhook_service.go
+++ b/backend/internal/services/webhook_service.go
@@ -15,11 +15,11 @@ import (
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- libcrypto "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/types/v2"
"github.com/getarcaneapp/arcane/types/v2/base"
"github.com/getarcaneapp/arcane/types/v2/updater"
webhooktypes "github.com/getarcaneapp/arcane/types/v2/webhook"
+ libcrypto "go.getarcane.app/sys/crypto"
"gorm.io/gorm"
)
diff --git a/backend/internal/services/webhook_service_test.go b/backend/internal/services/webhook_service_test.go
index 8f44d15ab..7aa72f46d 100644
--- a/backend/internal/services/webhook_service_test.go
+++ b/backend/internal/services/webhook_service_test.go
@@ -9,11 +9,11 @@ import (
"strings"
"testing"
- libcrypto "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/getarcaneapp/arcane/types/v2"
glsqlite "github.com/glebarez/sqlite"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ libcrypto "go.getarcane.app/sys/crypto"
"gorm.io/gorm"
"github.com/getarcaneapp/arcane/backend/v2/internal/database"
diff --git a/backend/pkg/libarcane/crypto/encryption.go b/backend/pkg/libarcane/crypto/encryption.go
deleted file mode 100644
index 38f80b169..000000000
--- a/backend/pkg/libarcane/crypto/encryption.go
+++ /dev/null
@@ -1,204 +0,0 @@
-package crypto
-
-import (
- "crypto/aes"
- "crypto/cipher"
- crand "crypto/rand"
- "crypto/sha256"
- "encoding/base64"
- "encoding/hex"
- "errors"
- "fmt"
- "io"
- "log/slog"
- "os"
- "path/filepath"
- "strings"
-
- "go.getarcane.app/sys/atomic"
-)
-
-var encryptionKey []byte
-
-type Config struct {
- EncryptionKey string
- Environment string
- AgentMode bool
-}
-
-// InitEncryption initializes the global encryption key based on configuration
-func InitEncryption(cfg *Config) {
- if keyStr := strings.TrimSpace(cfg.EncryptionKey); keyStr != "" {
- key, err := parseExplicitKey(keyStr)
- if err != nil {
- panic(err.Error())
- }
- encryptionKey = key
- if cfg.Environment != "production" {
- slog.Info("Encryption initialized from explicit ENCRYPTION_KEY", "env", cfg.Environment, "key_length_bytes", len(encryptionKey))
- }
- return
- }
-
- if cfg.AgentMode {
- key, err := loadOrCreateAgentKey()
- if err != nil {
- slog.Warn("Agent encryption key load/create returned an error; using in-memory key (may not persist)", "err", err)
- }
- encryptionKey = key
- return
- }
-
- if cfg.Environment == "production" {
- panic("ENCRYPTION_KEY is required in production environment")
- }
-
- encryptionKey = deriveDevKey()
- slog.Info("Encryption initialized (derived development key)", "env", cfg.Environment, "key_length_bytes", len(encryptionKey))
-}
-
-func parseExplicitKey(in string) ([]byte, error) {
- clean := strings.TrimSpace(in)
-
- // Try hex decoding first (most common: openssl rand -hex 32 produces 64 hex chars)
- if b, err := hex.DecodeString(clean); err == nil && len(b) >= 32 {
- return b[:32], nil
- }
-
- // Try base64 decoding (standard and raw)
- if b, err := base64.StdEncoding.DecodeString(clean); err == nil && len(b) >= 32 {
- return b[:32], nil
- }
- if b, err := base64.RawStdEncoding.DecodeString(clean); err == nil && len(b) >= 32 {
- return b[:32], nil
- }
-
- // Finally, try raw bytes (at least 32 ASCII characters)
- if len(clean) >= 32 {
- return []byte(clean[:32]), nil
- }
-
- return nil, fmt.Errorf("ENCRYPTION_KEY must be at least 32 bytes (raw/base64/hex). Provided=%d chars", len(clean))
-}
-
-func loadOrCreateAgentKey() ([]byte, error) {
- dir := agentKeyDir()
- if err := os.MkdirAll(dir, 0o700); err != nil {
- slog.Warn("Failed to create directory for agent encryption key; key may not persist", "dir", dir, "err", err)
- }
-
- keyPath := filepath.Join(dir, "agent_encryption_key")
-
- if data, err := os.ReadFile(keyPath); err == nil {
- if b, err := hex.DecodeString(strings.TrimSpace(string(data))); err == nil && len(b) == 32 {
- slog.Info("Loaded agent encryption key from disk", "path", keyPath)
- return b, nil
- }
- slog.Warn("Existing agent key invalid; will generate a new key", "path", keyPath)
- } else if !os.IsNotExist(err) {
- slog.Warn("Failed to read agent encryption key; will generate a new key", "path", keyPath, "err", err)
- }
-
- key := make([]byte, 32)
- if _, err := crand.Read(key); err != nil {
- slog.Warn("crypto/rand failed; falling back to deterministic key derivation (not ideal)", "err", err)
- sum := sha256.Sum256([]byte("arcane-agent-key"))
- key = sum[:]
- return key, nil
- }
-
- if err := atomicWriteHexFile(keyPath, key, 0o600); err != nil {
- slog.Warn("Failed to persist agent encryption key; using in-memory key (will regenerate next start)", "path", keyPath, "err", err)
- return key, err
- }
-
- slog.Info("Generated and persisted new agent encryption key", "path", keyPath)
- return key, nil
-}
-
-func agentKeyDir() string {
- if d, err := os.UserConfigDir(); err == nil && d != "" {
- return filepath.Join(d, "arcane")
- }
- if wd, err := os.Getwd(); err == nil && wd != "" {
- return filepath.Join(wd, ".arcane")
- }
- return os.TempDir()
-}
-
-func atomicWriteHexFile(path string, key []byte, mode os.FileMode) error {
- return atomic.WriteFile(path, []byte(hex.EncodeToString(key)+"\n"), mode)
-}
-
-func deriveDevKey() []byte {
- sum := sha256.Sum256([]byte("arcane-dev-key"))
- return sum[:]
-}
-
-// Encrypt encrypts a plaintext string using AES-GCM
-func Encrypt(plaintext string) (string, error) {
- if encryptionKey == nil {
- return "", errors.New("encryption not initialized - call InitEncryption first")
- }
-
- if plaintext == "" {
- return "", nil
- }
-
- block, err := aes.NewCipher(encryptionKey)
- if err != nil {
- return "", fmt.Errorf("failed to create cipher: %w", err)
- }
-
- gcm, err := cipher.NewGCM(block)
- if err != nil {
- return "", fmt.Errorf("failed to create GCM: %w", err)
- }
-
- nonce := make([]byte, gcm.NonceSize())
- if _, err = io.ReadFull(crand.Reader, nonce); err != nil {
- return "", fmt.Errorf("failed to generate nonce: %w", err)
- }
-
- ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
- return base64.StdEncoding.EncodeToString(ciphertext), nil
-}
-
-// Decrypt decrypts a base64 encoded ciphertext string using AES-GCM
-func Decrypt(ciphertext string) (string, error) {
- if encryptionKey == nil {
- return "", errors.New("encryption not initialized - call InitEncryption first")
- }
-
- if ciphertext == "" {
- return "", nil
- }
-
- data, err := base64.StdEncoding.DecodeString(ciphertext)
- if err != nil {
- return "", fmt.Errorf("failed to decode base64: %w", err)
- }
-
- block, err := aes.NewCipher(encryptionKey)
- if err != nil {
- return "", fmt.Errorf("failed to create cipher: %w", err)
- }
-
- gcm, err := cipher.NewGCM(block)
- if err != nil {
- return "", fmt.Errorf("failed to create GCM: %w", err)
- }
-
- nonceSize := gcm.NonceSize()
- if len(data) < nonceSize {
- return "", errors.New("ciphertext too short")
- }
-
- nonce, ciphertextBytes := data[:nonceSize], data[nonceSize:]
- plaintext, err := gcm.Open(nil, nonce, ciphertextBytes, nil)
- if err != nil {
- return "", fmt.Errorf("failed to decrypt: %w", err)
- }
-
- return string(plaintext), nil
-}
diff --git a/backend/pkg/libarcane/crypto/encryption_test.go b/backend/pkg/libarcane/crypto/encryption_test.go
deleted file mode 100644
index 5152a3f04..000000000
--- a/backend/pkg/libarcane/crypto/encryption_test.go
+++ /dev/null
@@ -1,324 +0,0 @@
-package crypto
-
-import (
- "encoding/base64"
- "testing"
-
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
-)
-
-func TestParseExplicitKey(t *testing.T) {
- tests := []struct {
- name string
- input string
- expectError bool
- description string
- }{
- {
- name: "valid hex key from openssl rand -hex 32",
- input: "e176e082991ba899f0687bdc00c4372f62ecfa2f021f6b226103166f2be775dd",
- expectError: false,
- description: "64 hex characters representing 32 bytes",
- },
- {
- name: "valid hex key uppercase",
- input: "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF",
- expectError: false,
- description: "Uppercase hex should work",
- },
- {
- name: "valid hex key with whitespace",
- input: " 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef \n",
- expectError: false,
- description: "Hex with leading/trailing whitespace should be trimmed",
- },
- {
- name: "valid base64 standard encoding",
- input: base64.StdEncoding.EncodeToString([]byte("01234567890123456789012345678901")),
- expectError: false,
- description: "Standard base64 encoded 32 bytes",
- },
- {
- name: "valid base64 raw encoding",
- input: base64.RawStdEncoding.EncodeToString([]byte("01234567890123456789012345678901")),
- expectError: false,
- description: "Raw base64 encoded 32 bytes (no padding)",
- },
- {
- name: "valid raw 32 byte string",
- input: "01234567890123456789012345678901",
- expectError: false,
- description: "Exactly 32 ASCII characters",
- },
- {
- name: "invalid hex key too short",
- input: "0123456789abcdef",
- expectError: true,
- description: "16 hex characters (8 bytes) - too short",
- },
- {
- name: "valid hex key longer than 32 bytes uses first 32",
- input: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef00",
- expectError: false,
- description: "66 hex characters (33 bytes) - uses first 32 bytes",
- },
- {
- name: "invalid raw string too short",
- input: "tooshort",
- expectError: true,
- description: "Less than 32 characters",
- },
- {
- name: "valid raw string longer than 32 uses first 32",
- input: "012345678901234567890123456789012",
- expectError: false,
- description: "33 characters - uses first 32",
- },
- {
- name: "invalid base64 wrong length",
- input: base64.StdEncoding.EncodeToString([]byte("short")),
- expectError: true,
- description: "Base64 of less than 32 bytes",
- },
- {
- name: "empty string",
- input: "",
- expectError: true,
- description: "Empty encryption key",
- },
- {
- name: "whitespace only",
- input: " \n\t ",
- expectError: true,
- description: "Only whitespace",
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- key, err := parseExplicitKey(tt.input)
-
- if tt.expectError {
- require.Error(t, err, tt.description)
- assert.Nil(t, key)
- } else {
- require.NoError(t, err, tt.description)
- assert.NotNil(t, key)
- assert.Len(t, key, 32, "Key must be exactly 32 bytes")
- }
- })
- }
-}
-
-func TestInitEncryption(t *testing.T) {
- t.Run("with explicit hex key", func(t *testing.T) {
- hexKey := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
- cfg := &Config{
- EncryptionKey: hexKey,
- Environment: "production",
- }
-
- InitEncryption(cfg)
-
- assert.NotNil(t, encryptionKey)
- assert.Len(t, encryptionKey, 32)
- })
-
- t.Run("with explicit base64 key", func(t *testing.T) {
- rawKey := []byte("01234567890123456789012345678901")
- b64Key := base64.StdEncoding.EncodeToString(rawKey)
- cfg := &Config{
- EncryptionKey: b64Key,
- Environment: "production",
- }
-
- InitEncryption(cfg)
-
- assert.NotNil(t, encryptionKey)
- assert.Len(t, encryptionKey, 32)
- })
-
- t.Run("with explicit raw key", func(t *testing.T) {
- cfg := &Config{
- EncryptionKey: "01234567890123456789012345678901",
- Environment: "production",
- }
-
- InitEncryption(cfg)
-
- assert.NotNil(t, encryptionKey)
- assert.Len(t, encryptionKey, 32)
- })
-
- t.Run("dev mode with no key", func(t *testing.T) {
- cfg := &Config{
- EncryptionKey: "",
- Environment: "development",
- AgentMode: false,
- }
-
- InitEncryption(cfg)
-
- assert.NotNil(t, encryptionKey)
- assert.Len(t, encryptionKey, 32)
- })
-
- t.Run("production mode without key panics", func(t *testing.T) {
- cfg := &Config{
- EncryptionKey: "",
- Environment: "production",
- AgentMode: false,
- }
-
- assert.Panics(t, func() {
- InitEncryption(cfg)
- }, "Should panic when no encryption key provided in production")
- })
-
- t.Run("invalid key panics", func(t *testing.T) {
- cfg := &Config{
- EncryptionKey: "tooshort",
- Environment: "production",
- }
-
- assert.Panics(t, func() {
- InitEncryption(cfg)
- }, "Should panic with invalid encryption key")
- })
-}
-
-func TestEncryptDecrypt(t *testing.T) {
- hexKey := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
- cfg := &Config{
- EncryptionKey: hexKey,
- Environment: "test",
- }
- InitEncryption(cfg)
-
- t.Run("encrypt and decrypt simple text", func(t *testing.T) {
- plaintext := "secret password"
-
- encrypted, err := Encrypt(plaintext)
- require.NoError(t, err)
- assert.NotEmpty(t, encrypted)
- assert.NotEqual(t, plaintext, encrypted)
-
- decrypted, err := Decrypt(encrypted)
- require.NoError(t, err)
- assert.Equal(t, plaintext, decrypted)
- })
-
- t.Run("encrypt and decrypt empty string", func(t *testing.T) {
- encrypted, err := Encrypt("")
- require.NoError(t, err)
- assert.Empty(t, encrypted)
-
- decrypted, err := Decrypt("")
- require.NoError(t, err)
- assert.Empty(t, decrypted)
- })
-
- t.Run("encrypt and decrypt long text", func(t *testing.T) {
- plaintext := "This is a much longer secret text with special characters: !@#$%^&*()_+-=[]{}|;:',.<>?/~`"
-
- encrypted, err := Encrypt(plaintext)
- require.NoError(t, err)
- assert.NotEmpty(t, encrypted)
-
- decrypted, err := Decrypt(encrypted)
- require.NoError(t, err)
- assert.Equal(t, plaintext, decrypted)
- })
-
- t.Run("encrypt and decrypt unicode text", func(t *testing.T) {
- plaintext := "Unicode test: hello world with emoji"
-
- encrypted, err := Encrypt(plaintext)
- require.NoError(t, err)
- assert.NotEmpty(t, encrypted)
-
- decrypted, err := Decrypt(encrypted)
- require.NoError(t, err)
- assert.Equal(t, plaintext, decrypted)
- })
-
- t.Run("encrypt same text twice produces different ciphertexts", func(t *testing.T) {
- plaintext := "secret"
-
- encrypted1, err := Encrypt(plaintext)
- require.NoError(t, err)
-
- encrypted2, err := Encrypt(plaintext)
- require.NoError(t, err)
-
- assert.NotEqual(t, encrypted1, encrypted2, "Same plaintext should produce different ciphertexts due to random nonce")
-
- decrypted1, err := Decrypt(encrypted1)
- require.NoError(t, err)
- assert.Equal(t, plaintext, decrypted1)
-
- decrypted2, err := Decrypt(encrypted2)
- require.NoError(t, err)
- assert.Equal(t, plaintext, decrypted2)
- })
-
- t.Run("decrypt invalid base64 fails", func(t *testing.T) {
- _, err := Decrypt("not-valid-base64!!!")
- require.Error(t, err)
- assert.Contains(t, err.Error(), "failed to decode base64")
- })
-
- t.Run("decrypt corrupted ciphertext fails", func(t *testing.T) {
- encrypted, err := Encrypt("test")
- require.NoError(t, err)
-
- corrupted := encrypted[:len(encrypted)-4] + "XXXX"
-
- _, err = Decrypt(corrupted)
- assert.Error(t, err)
- })
-
- t.Run("decrypt with wrong key fails", func(t *testing.T) {
- plaintext := "secret data"
-
- encrypted, err := Encrypt(plaintext)
- require.NoError(t, err)
-
- differentKey := "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
- differentCfg := &Config{
- EncryptionKey: differentKey,
- Environment: "test",
- }
- InitEncryption(differentCfg)
-
- _, err = Decrypt(encrypted)
- require.Error(t, err)
- assert.Contains(t, err.Error(), "failed to decrypt")
- })
-}
-
-func TestEncryptDecryptWithoutInit(t *testing.T) {
- encryptionKey = nil
-
- t.Run("encrypt without initialization fails", func(t *testing.T) {
- _, err := Encrypt("test")
- require.Error(t, err)
- assert.Contains(t, err.Error(), "encryption not initialized")
- })
-
- t.Run("decrypt without initialization fails", func(t *testing.T) {
- _, err := Decrypt("dGVzdA==")
- require.Error(t, err)
- assert.Contains(t, err.Error(), "encryption not initialized")
- })
-}
-
-func TestDeriveDevKey(t *testing.T) {
- key := deriveDevKey()
- assert.NotNil(t, key)
- assert.Len(t, key, 32, "Derived dev key must be 32 bytes")
-
- key2 := deriveDevKey()
- assert.Equal(t, key, key2, "Derived dev key should be deterministic")
-}
diff --git a/backend/pkg/libarcane/edge/tls.go b/backend/pkg/libarcane/edge/tls.go
index fc6e47d51..4f01fbd59 100644
--- a/backend/pkg/libarcane/edge/tls.go
+++ b/backend/pkg/libarcane/edge/tls.go
@@ -26,9 +26,9 @@ import (
"time"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
- libcrypto "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
certgen "github.com/getarcaneapp/arcane/cli/v2/pkg/generate"
"go.getarcane.app/sys/atomic"
+ libcrypto "go.getarcane.app/sys/crypto"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
)
diff --git a/backend/pkg/libarcane/edge/tls_test.go b/backend/pkg/libarcane/edge/tls_test.go
index ac712b062..d8e7bc162 100644
--- a/backend/pkg/libarcane/edge/tls_test.go
+++ b/backend/pkg/libarcane/edge/tls_test.go
@@ -20,8 +20,8 @@ import (
"time"
"github.com/getarcaneapp/arcane/backend/v2/internal/common"
- libcrypto "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
"github.com/stretchr/testify/require"
+ libcrypto "go.getarcane.app/sys/crypto"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
)
diff --git a/backend/pkg/utils/jwtclaims/jwt.go b/backend/pkg/utils/jwtclaims/jwt.go
index bc6fda269..ca25f2aa0 100644
--- a/backend/pkg/utils/jwtclaims/jwt.go
+++ b/backend/pkg/utils/jwtclaims/jwt.go
@@ -96,7 +96,7 @@ const (
//
// When requireExplicit is true (production manager), a real secret is mandatory:
// an empty, default, or too-short JWT_SECRET panics at startup — mirroring the
-// ENCRYPTION_KEY guard in libarcane/crypto. Otherwise (development / agent mode)
+// ENCRYPTION_KEY guard in go.getarcane.app/sys/crypto. Otherwise (development / agent mode)
// a random per-boot key is generated when none (or only the public default) is
// configured, so the public default never becomes a live signing key.
func CheckOrGenerateJwtSecret(jwtSecret string, requireExplicit bool) []byte {
diff --git a/backend/pkg/utils/notifications/config_credentials.go b/backend/pkg/utils/notifications/config_credentials.go
index daaf5a272..9194ba699 100644
--- a/backend/pkg/utils/notifications/config_credentials.go
+++ b/backend/pkg/utils/notifications/config_credentials.go
@@ -8,7 +8,7 @@ import (
"log/slog"
"github.com/getarcaneapp/arcane/backend/v2/internal/models"
- "github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane/crypto"
+ "go.getarcane.app/sys/crypto"
)
// DecodeConfig round-trips a provider config (models.JSON) into a typed struct T.
diff --git a/go.work.sum b/go.work.sum
index ea6f95e0d..73a6583fa 100644
--- a/go.work.sum
+++ b/go.work.sum
@@ -2084,6 +2084,8 @@ go.etcd.io/raft/v3 v3.6.0 h1:5NtvbDVYpnfZWcIHgGRk9DyzkBIXOi8j+DDp1IcnUWQ=
go.etcd.io/raft/v3 v3.6.0/go.mod h1:nLvLevg6+xrVtHUmVaTcTz603gQPHfh7kUAwV6YpfGo=
go.getarcane.app/docker/convert v0.1.0 h1:prlIOs6dMx6CdnetsJFZ6ak0vcistM3DcH5lrZiFHBU=
go.getarcane.app/docker/convert v0.1.0/go.mod h1:o9y4z/vah8HiHFtI+jJVFHbs8nLrkARYt88OaUlvGig=
+go.getarcane.app/sys/crypto v0.1.0 h1:HDQ2CKHkm+wZe5xb/qR7MzW5fKi0Wq8Jcyr7qDVgRWg=
+go.getarcane.app/sys/crypto v0.1.0/go.mod h1:YtoRB0TENwfdNl0ZUTWkeas/8rT7NhCbI8ENsZzCCdU=
go.getarcane.app/updater v0.3.6 h1:+BqoycaRsHj+cLVSzcYQuqbHxUKbtmaozMNblz5bVSw=
go.getarcane.app/updater v0.3.6/go.mod h1:2ENitZR/lA5MuBn60OZYR5wvU3ZCE+pAgfrebfOlRDA=
go.getarcane.app/updater v0.3.7 h1:WqHsxudvt8gvCR7iSHdxdzwH0+hM0FAh64bSypBX9Mo=