From 92920d5d02168d35e1b7e24c14a514305949df3e Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 2 Feb 2023 19:25:35 +0800 Subject: [PATCH] [add] migrate cert --- Makefile | 2 +- cert/cert.go | 7 ++++++- cert/cert_test.go | 26 ++++++++++++++++---------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index eb32d9f2..8db370bb 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ COMMIT := $(shell git log | head -n 1 | awk '{print substr($$2, 0, 12)}') NOW := $(shell date +"%FT%T%z") # ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) -TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector|query|plugin' | grep -vE 'examples|tests*|config|widgets') +TESTFOLDER := $(shell $(GO) list ./... | grep -E 'api|model|flow|script|fs|i18n|connector|query|plugin|cert' | grep -vE 'examples|tests*|config|widgets') TESTTAGS ?= "" # TESTWIDGETS := $(shell $(GO) list ./widgets/...) diff --git a/cert/cert.go b/cert/cert.go index 2cf2c651..39a0329f 100644 --- a/cert/cert.go +++ b/cert/cert.go @@ -1,6 +1,8 @@ package cert import ( + "path/filepath" + "github.com/yaoapp/gou/application" "github.com/yaoapp/gou/ssl" "github.com/yaoapp/yao/config" @@ -11,7 +13,10 @@ import ( func Load(cfg config.Config) error { exts := []string{"*.pem", "*.key", "*.pub"} return application.App.Walk("certs", func(root, file string, isdir bool) error { - _, err := ssl.Load(file, share.ID(root, file)) + if isdir { + return nil + } + _, err := ssl.Load(file, share.ID(root, file)+filepath.Ext(file)) return err }, exts...) } diff --git a/cert/cert_test.go b/cert/cert_test.go index fd4d3518..e94662ba 100644 --- a/cert/cert_test.go +++ b/cert/cert_test.go @@ -7,16 +7,30 @@ import ( "github.com/yaoapp/gou/process" "github.com/yaoapp/gou/ssl" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/test" ) func TestLoad(t *testing.T) { + test.Prepare(t, config.Conf) + defer test.Clean() + Load(config.Conf) check(t) } +func check(t *testing.T) { + ids := map[string]bool{} + for id := range ssl.Certificates { + ids[id] = true + } + assert.True(t, ids["cert.pem"]) + assert.True(t, ids["cert.key"]) + assert.True(t, ids["cert.pub"]) +} + func TestProcessSign(t *testing.T) { Load(config.Conf) - args := []interface{}{"hello world", "private", "SHA256"} + args := []interface{}{"hello world", "cert.key", "SHA256"} signature, err := process.New("ssl.Sign", args...).Exec() if err != nil { t.Fatal(err) @@ -27,7 +41,7 @@ func TestProcessSign(t *testing.T) { func TestProcessVerify(t *testing.T) { Load(config.Conf) signature := "EDHf3C9TXEk7y8LzIk5czLefXZyGxcMDVMcbNuBBegDkTqnPsRQnhFtNOgCdox8lI3MzLatwjoljoMY4Qk+sHGd5mAHMpiREa1gRFSVYpA2xvXZ3+KsfOHAdICQrfUdy59QaJGo6iGPNGG8PQOXHPTVNn6LMfryat9+f4l21DPAZiT0RyCUgFZE3/Qv8Z/6J4AsIXMSKZD6BGPPHUxGe7UBrXZvcR5dX25EiNjuH2OO38YJnDiTRVw14UI5fk/mQrwRdezj5tSKFCyHt912BZExXtkHISiYFNTZ/2RhOup5Xx6o3GvrEOdshrnN80Lwu1Aaju+lnZp13hDz4P6hU7w==" - args := []interface{}{"hello world", signature, "cert", "SHA256"} + args := []interface{}{"hello world", signature, "cert.pem", "SHA256"} res, err := process.New("ssl.Verify", args...).Exec() if err != nil { t.Fatal(err) @@ -35,11 +49,3 @@ func TestProcessVerify(t *testing.T) { assert.True(t, res.(bool)) } - -func check(t *testing.T) { - keys := []string{} - for key := range ssl.Certificates { - keys = append(keys, key) - } - assert.Equal(t, 3, len(keys)) -}