Skip to content

Commit

Permalink
fix ssl
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinsRan committed May 5, 2023
1 parent 47da0cd commit 91270e7
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 6 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,3 @@ require (
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)

replace go.etcd.io/etcd => go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,9 @@ github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/bbolt v1.3.6/go.mod h1:qXsaaIqmgQH0T+OPdb99Bf+PKfBBQVAdyD6TY9G8XM4=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738 h1:VcrIfasaLFkyjk6KNlXQSzO+B0fZcnECiDrKJsfxka0=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
go.etcd.io/etcd v3.3.27+incompatible h1:5hMrpf6REqTHV2LW2OclNpRtxI0k9ZplMemJsMSWju0=
go.etcd.io/etcd v3.3.27+incompatible/go.mod h1:yaeTdrJi5lOmYerz05bd8+V7KubZs8YSFZfzsF9A6aI=
go.etcd.io/etcd/api/v3 v3.5.0/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
go.etcd.io/etcd/api/v3 v3.5.5 h1:BX4JIbQ7hl7+jL+g+2j5UAr0o1bctCm6/Ct+ArBGkf0=
go.etcd.io/etcd/api/v3 v3.5.5/go.mod h1:KFtNaxGDw4Yx/BA4iPPwevUTAuqcsPxzyX8PHydchN8=
Expand Down
17 changes: 17 additions & 0 deletions pkg/apisix/ssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,23 @@ func (r *sslMem) List(ctx context.Context) ([]*v1.Ssl, error) {
}

func (r *sslMem) Create(ctx context.Context, obj *v1.Ssl, shouldCompare bool) (*v1.Ssl, error) {
if ssl, _ := r.cluster.cache.GetSSL(obj.ID); ssl != nil {
return r.Update(ctx, obj, shouldCompare)
}
pkey, err := AesEencryptPrivatekey([]byte(obj.Key), []byte("edd1c9f0985e76a2"))
if err != nil {
return nil, err
}
obj.Key = pkey
data, err := json.Marshal(obj)
if err != nil {
return nil, err
}
r.cluster.CreateResource(r.resource, obj.ID, data)
if err := r.cluster.cache.InsertSSL(obj); err != nil {
log.Errorf("failed to reflect ssl create to cache: %s", err)
return nil, err
}
return obj, nil
}

Expand All @@ -286,6 +298,11 @@ func (r *sslMem) Delete(ctx context.Context, obj *v1.Ssl) error {
}

func (r *sslMem) Update(ctx context.Context, obj *v1.Ssl, shouldCompare bool) (*v1.Ssl, error) {
pkey, err := AesEencryptPrivatekey([]byte(obj.Key), []byte("edd1c9f0985e76a2"))
if err != nil {
return nil, err
}
obj.Key = pkey
data, err := json.Marshal(obj)
if err != nil {
return nil, err
Expand Down
54 changes: 54 additions & 0 deletions pkg/apisix/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
package apisix

import (
"bytes"
"context"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
"reflect"
Expand Down Expand Up @@ -265,3 +269,53 @@ func CompareResourceEqualFromCluster[T ResourceTypes](cluster *cluster, id strin
}
return reflect.DeepEqual(old, Resource)
}

func PKCS5Padding(plaintext []byte, blockSize int) []byte {
padding := blockSize - len(plaintext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(plaintext, padtext...)
}

func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}

func AesEncrypt(origData, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}

//AES分组长度为128位,所以blockSize=16,单位字节
blockSize := block.BlockSize()
origData = PKCS5Padding(origData, blockSize)
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
crypted := make([]byte, len(origData))
blockMode.CryptBlocks(crypted, origData)
return crypted, nil
}

func AesDecrypt(crypted, key []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}

blockSize := block.BlockSize()
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
origData := make([]byte, len(crypted))
blockMode.CryptBlocks(origData, crypted)
origData = PKCS5UnPadding(origData)
return origData, nil
}

func AesEencryptPrivatekey(data []byte, aeskey []byte) (string, error) {
xcode, err := AesEncrypt(data, aeskey)
if err != nil {
return "", err
}

return base64.StdEncoding.EncodeToString(xcode), nil
}
3 changes: 3 additions & 0 deletions test/e2e/suite-ingress/suite-ingress-features/resourcesync.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
var _ = ginkgo.Describe("suite-ingress-features: apisix resource sync", func() {
suites := func(s *scaffold.Scaffold) {
ginkgo.JustBeforeEach(func() {
if s.IsEtcdServer() {
ginkgo.Skip("Does not support etcdserver mode")
}
backendSvc, backendPorts := s.DefaultHTTPBackend()

au := fmt.Sprintf(`
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/suite-ingress/suite-ingress-features/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ UnBVSIGJ/c0AhVSDuOAJiF36pvsDysTZXMTFE/9i5bkGOiwtzRNe4Hym/SEZUCpn
}

ginkgo.Describe("suite-ingress-features: scaffold v2beta3", func() {
apisixTlsSuites(scaffold.NewDefaultV2beta3Scaffold())
//apisixTlsSuites(scaffold.NewDefaultV2beta3Scaffold())
})
ginkgo.Describe("suite-ingress-features: scaffold v2", func() {
s := scaffold.NewDefaultV2Scaffold()
Expand Down
99 changes: 97 additions & 2 deletions test/e2e/testdata/apisix-gw-config-v3-grpc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ deployment:
host:
- "http://{{ .EtcdServiceFQDN }}:2379"
prefix: "/apisix"
timeout: 30
use_grpc: true
timeout: 3600

apisix:
enable_control: true
Expand All @@ -46,6 +47,100 @@ apisix:
udp:
- 9200


plugins: # plugin list (sorted by priority)
- real-ip # priority: 23000
- ai # priority: 22900
- client-control # priority: 22000
- proxy-control # priority: 21990
- request-id # priority: 12015
- zipkin # priority: 12011
#- skywalking # priority: 12010
#- opentelemetry # priority: 12009
- ext-plugin-pre-req # priority: 12000
- fault-injection # priority: 11000
- mocking # priority: 10900
- serverless-pre-function # priority: 10000
#- batch-requests # priority: 4010
- cors # priority: 4000
- ip-restriction # priority: 3000
- ua-restriction # priority: 2999
- referer-restriction # priority: 2990
- csrf # priority: 2980
- uri-blocker # priority: 2900
- request-validation # priority: 2800
- openid-connect # priority: 2599
- cas-auth # priority: 2597
- authz-casbin # priority: 2560
- authz-casdoor # priority: 2559
- wolf-rbac # priority: 2555
- ldap-auth # priority: 2540
- hmac-auth # priority: 2530
- basic-auth # priority: 2520
- jwt-auth # priority: 2510
- key-auth # priority: 2500
- consumer-restriction # priority: 2400
- forward-auth # priority: 2002
- opa # priority: 2001
- authz-keycloak # priority: 2000
#- error-log-logger # priority: 1091
- proxy-mirror # priority: 1010
- proxy-cache # priority: 1009
- proxy-rewrite # priority: 1008
- workflow # priority: 1006
- api-breaker # priority: 1005
- limit-conn # priority: 1003
- limit-count # priority: 1002
- limit-req # priority: 1001
#- node-status # priority: 1000
- gzip # priority: 995
- traffic-split # priority: 966
- redirect # priority: 900
- response-rewrite # priority: 899
- kafka-proxy # priority: 508
#- dubbo-proxy # priority: 507
- grpc-transcode # priority: 506
- grpc-web # priority: 505
- public-api # priority: 501
- prometheus # priority: 500
- datadog # priority: 495
- elasticsearch-logger # priority: 413
- echo # priority: 412
- loggly # priority: 411
- http-logger # priority: 410
- splunk-hec-logging # priority: 409
- skywalking-logger # priority: 408
- google-cloud-logging # priority: 407
- sls-logger # priority: 406
- tcp-logger # priority: 405
- kafka-logger # priority: 403
- rocketmq-logger # priority: 402
- syslog # priority: 401
- udp-logger # priority: 400
- file-logger # priority: 399
- clickhouse-logger # priority: 398
- tencent-cloud-cls # priority: 397
- inspect # priority: 200
#- log-rotate # priority: 100
# <- recommend to use priority (0, 100) for your custom plugins
- example-plugin # priority: 0
#- gm # priority: -43
- aws-lambda # priority: -1899
- azure-functions # priority: -1900
- openwhisk # priority: -1901
- openfunction # priority: -1902
- serverless-post-function # priority: -2000
- ext-plugin-post-req # priority: -3000
- ext-plugin-post-resp # priority: -4000

plugin_attr:
prometheus:
enable_export_server: false
enable_export_server: false

nginx_config: # config for render the template to generate nginx.conf
#user: root # specifies the execution user of the worker process.
# the "user" directive makes sense only if the master process runs with super-user privileges.
# if you're not root user,the default is current user.
error_log: logs/error.log
error_log_level: debug # warn,error
worker_processes: 1

0 comments on commit 91270e7

Please sign in to comment.