Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support TLS #95

Merged
merged 12 commits into from
Dec 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/ingress/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func NewIngressCommand() *cobra.Command {
c.ApisixUpstream()
// ApisixService
c.ApisixService()
// ApisixTLS
c.ApisixTLS()

go func() {
time.Sleep(time.Duration(10) * time.Second)
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ require (
github.com/gin-gonic/gin v1.6.3
github.com/gogo/protobuf v1.3.1 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/gxthrj/apisix-ingress-types v0.1.2
github.com/gxthrj/apisix-types v0.1.0
github.com/gxthrj/seven v0.1.9
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/gxthrj/apisix-ingress-types v0.1.3
github.com/gxthrj/apisix-types v0.1.3
github.com/gxthrj/seven v0.2.4
github.com/julienschmidt/httprouter v1.3.0
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.4.0
go.uber.org/zap v1.13.0
Expand All @@ -19,6 +19,6 @@ require (
k8s.io/client-go v0.0.0-20190819141724-e14f31a72a77
)

replace github.com/gxthrj/apisix-ingress-types v0.1.2 => github.com/api7/ingress-types v0.1.2
replace github.com/gxthrj/apisix-ingress-types v0.1.3 => github.com/api7/ingress-types v0.1.3

replace github.com/gxthrj/apisix-types v0.1.0 => github.com/api7/types v0.1.0
replace github.com/gxthrj/apisix-types v0.1.2 => github.com/api7/types v0.1.2
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/api7/ingress-types v0.1.2 h1:K8GxVKSvPi89CHozico/oLbcy6mZOdeOk+VfJRCLXrE=
github.com/api7/ingress-types v0.1.2/go.mod h1:xWuHLSHGN4/JZjz9b0ftKgtE3yZ7NehkJAiTyZ+KoPA=
github.com/api7/ingress-types v0.1.3 h1:euvLySusNeOjzOjnpZl+J3Ve5ZcWoby/lm3lPmH4xTI=
github.com/api7/ingress-types v0.1.3/go.mod h1:xWuHLSHGN4/JZjz9b0ftKgtE3yZ7NehkJAiTyZ+KoPA=
github.com/api7/types v0.1.0 h1:x8nOUY9yjNCCpxlODqXpp5MrEjp9W4grNyGsgxxLq1Y=
github.com/api7/types v0.1.0/go.mod h1:STdHATWxqIeJhUEXt6dZTd6z0To0qO70K/1mnfBtlC0=
github.com/api7/types v0.1.1/go.mod h1:STdHATWxqIeJhUEXt6dZTd6z0To0qO70K/1mnfBtlC0=
github.com/api7/types v0.1.2/go.mod h1:STdHATWxqIeJhUEXt6dZTd6z0To0qO70K/1mnfBtlC0=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
Expand Down Expand Up @@ -113,8 +117,16 @@ github.com/gregjones/httpcache v0.0.0-20170728041850-787624de3eb7/go.mod h1:Fecb
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/gxthrj/apisix-ingress-types v0.1.3/go.mod h1:xWuHLSHGN4/JZjz9b0ftKgtE3yZ7NehkJAiTyZ+KoPA=
github.com/gxthrj/apisix-types v0.1.0/go.mod h1:STdHATWxqIeJhUEXt6dZTd6z0To0qO70K/1mnfBtlC0=
github.com/gxthrj/apisix-types v0.1.1/go.mod h1:STdHATWxqIeJhUEXt6dZTd6z0To0qO70K/1mnfBtlC0=
github.com/gxthrj/apisix-types v0.1.3 h1:H8TqK6uIcG+rM2o+wq33gSh4KKZI+197j9lcy8aZ0Iw=
github.com/gxthrj/apisix-types v0.1.3/go.mod h1:STdHATWxqIeJhUEXt6dZTd6z0To0qO70K/1mnfBtlC0=
github.com/gxthrj/seven v0.1.9 h1:EccFVE5PIqZXyt91laXxGu9tydCjg31EXn/Q4aFfwD4=
github.com/gxthrj/seven v0.1.9/go.mod h1:epDVCYT1ibfV6fgaeM918QAer9u0/M2LXW6OcIRdfQc=
github.com/gxthrj/seven v0.2.0 h1:S85ZI2TzVQILsiI9BE+Sa0VqrkU3kzNYJFOOh4+5Vu8=
github.com/gxthrj/seven v0.2.0/go.mod h1:Uf0JHSRmhZyV3tPLV1oVzq/Dw19ya9rXFsECiLKrgVk=
github.com/gxthrj/seven v0.2.4/go.mod h1:SYs/veqEMdwRF5BL3nf/nxfypoDMO2E6Odgp17m+J9U=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down Expand Up @@ -157,6 +169,7 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
Expand Down
68 changes: 68 additions & 0 deletions pkg/ingress/apisix/tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apisix

import (
ingressConf "github.com/api7/ingress-controller/pkg/kube"
tokers marked this conversation as resolved.
Show resolved Hide resolved
ingress "github.com/gxthrj/apisix-ingress-types/pkg/apis/config/v1"
apisix "github.com/gxthrj/apisix-types/pkg/apis/apisix/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
ApisixTls = "ApisixTls"
gxthrj marked this conversation as resolved.
Show resolved Hide resolved
)

type ApisixTlsCRD ingress.ApisixTls

// Convert convert to apisix.Ssl from ingress.ApisixTls CRD
func (as *ApisixTlsCRD) Convert(sc Secreter) (*apisix.Ssl, error) {
name := as.Name
namespace := as.Namespace
id := namespace + "_" + name
secretName := as.Spec.Secret.Name
secretNamespace := as.Spec.Secret.Namespace
secret, err := sc.FindByName(secretNamespace, secretName)
if err != nil {
return nil, err
}
cert := string(secret.Data["cert"])
key := string(secret.Data["key"])
status := 1
snis := make([]*string, 0)
for _, host := range as.Spec.Hosts {
snis = append(snis, &host)
}
ssl := &apisix.Ssl{
ID: &id,
Snis: snis,
Cert: &cert,
Key: &key,
Status: &status,
}
return ssl, nil
}

type Secreter interface {
FindByName(namespace, name string) (*v1.Secret, error)
}

type SecretClient struct{}

func (sc *SecretClient) FindByName(namespace, name string) (*v1.Secret, error) {
clientSet := ingressConf.GetKubeClient()
return clientSet.CoreV1().Secrets(namespace).Get(name, metav1.GetOptions{})
}
115 changes: 115 additions & 0 deletions pkg/ingress/apisix/tls_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package apisix

import (
"encoding/json"
"fmt"
"testing"

a6Type "github.com/gxthrj/apisix-types/pkg/apis/apisix/v1"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"k8s.io/api/core/v1"
)

func TestConvert(t *testing.T) {
atlsStr := `
apiVersion: apisix.apache.org/v1
kind: ApisixTls
metadata:
name: foo
namespace: helm
spec:
hosts:
- api6.com
secret:
name: test-atls
namespace: helm
`
id := "helm_foo"
host := "api6.com"
snis := []*string{&host}
status := int(1)
cert := "root"
key := "123456"
sslExpect := &a6Type.Ssl{
ID: &id,
Snis: snis,
Cert: &cert,
Key: &key,
Status: &status,
}
atlsCRD := &ApisixTlsCRD{}
err := yaml.Unmarshal([]byte(atlsStr), atlsCRD)
assert.Nil(t, err, "yaml decode failed")
sc := &SecretClientMock{}
ssl, err := atlsCRD.Convert(sc)
assert.EqualValues(t, sslExpect.Key, ssl.Key, "ssl convert error")
assert.EqualValues(t, sslExpect.ID, ssl.ID, "ssl convert error")
assert.EqualValues(t, sslExpect.Cert, ssl.Cert, "ssl convert error")
assert.EqualValues(t, sslExpect.Snis, ssl.Snis, "ssl convert error")
}

func TestConvert_Error(t *testing.T) {
atlsStr := `
apiVersion: apisix.apache.org/v1
kind: ApisixTls
metadata:
name: foo
namespace: helm
spec:
secret:
name: test-atls
namespace: helm
`
atlsCRD := &ApisixTlsCRD{}
err := yaml.Unmarshal([]byte(atlsStr), atlsCRD)
assert.Nil(t, err, "yaml decode failed")
sc := &SecretClientErrorMock{}
ssl, err := atlsCRD.Convert(sc)
assert.Nil(t, ssl)
assert.NotNil(t, err)
}

type SecretClientMock struct{}

func (sc *SecretClientMock) FindByName(namespace, name string) (*v1.Secret, error) {
secretStr := `
{
"apiVersion": "v1",
"kind": "Secret",
"metadata": {
"name": "test-atls",
"namespace": "helm"
},
"data": {
"cert": "cm9vdA==",
"key": "MTIzNDU2"
}
}
`
secret := &v1.Secret{}
if err := json.Unmarshal([]byte(secretStr), secret); err != nil {
fmt.Errorf(err.Error())
}
return secret, nil
}

type SecretClientErrorMock struct{}

func (sc *SecretClientErrorMock) FindByName(namespace, name string) (*v1.Secret, error) {
return nil, fmt.Errorf("NOT FOUND")
}
Loading