Skip to content

Commit

Permalink
added fill and extract
Browse files Browse the repository at this point in the history
  • Loading branch information
kofoworola committed Jan 21, 2025
1 parent 5a6a544 commit 6f6eb36
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
14 changes: 14 additions & 0 deletions apidef/oas/upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ type TLSTransport struct {
ForceCommonNameCheck bool `bson:"forceCommonNameCheck,omitempty" json:"forceCommonNameCheck,omitempty"`
}

// Fill fills *TLSTransport from apidef.ServiceDiscoveryConfiguration.
func (t *TLSTransport) Fill(api apidef.APIDefinition) {
t.ForceCommonNameCheck = api.Proxy.Transport.SSLForceCommonNameCheck
t.Ciphers = api.Proxy.Transport.SSLCipherSuites
Expand All @@ -251,6 +252,7 @@ func (t *TLSTransport) Fill(api apidef.APIDefinition) {
t.InsecureSkipVerify = api.Proxy.Transport.SSLInsecureSkipVerify
}

// ExtractTo extracts *TLSTransport into *apidef.ServiceDiscoveryConfiguration.
func (t *TLSTransport) ExtractTo(api *apidef.APIDefinition) {
api.Proxy.Transport.SSLForceCommonNameCheck = t.ForceCommonNameCheck
api.Proxy.Transport.SSLCipherSuites = t.Ciphers
Expand All @@ -259,6 +261,7 @@ func (t *TLSTransport) ExtractTo(api *apidef.APIDefinition) {
api.Proxy.Transport.SSLInsecureSkipVerify = t.InsecureSkipVerify
}

// tlsVersionFromString converts v in the form of 1.2/1.3 to the version int
func tlsVersionFromString(v string) uint16 {
switch v {
case "1.0":
Expand All @@ -274,6 +277,7 @@ func tlsVersionFromString(v string) uint16 {
}
}

// tlsVersionFromString converts v from version into to the form 1.0/1.1
func tlsVersionToString(v uint16) string {
switch v {
case tls.VersionTLS10:
Expand All @@ -300,6 +304,16 @@ type Proxy struct {
URL string `bson:"url" json:"url"`
}

// Fill fills *Proxy from apidef.ServiceDiscoveryConfiguration.
func (p *Proxy) Fill(api apidef.APIDefinition) {
p.URL = api.Proxy.Transport.ProxyURL
}

// ExtractTo extracts *Proxy into *apidef.ServiceDiscoveryConfiguration.
func (p *Proxy) ExtractTo(api *apidef.APIDefinition) {
api.Proxy.Transport.ProxyURL = p.URL
}

// ServiceDiscovery holds configuration required for service discovery.
type ServiceDiscovery struct {
// Enabled activates Service Discovery.
Expand Down
47 changes: 45 additions & 2 deletions apidef/oas/upstream_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package oas

import (
"crypto/tls"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -472,11 +473,53 @@ func TestCertificatePinning(t *testing.T) {
})
}

func TestTLSTransport(t *testing.T) {
func TestTLSTransportProxy(t *testing.T) {
t.Run("with tls settings", func(t *testing.T) {
transport := TLSTransport{
InsecureSkipVerify: false,
InsecureSkipVerify: true,
MinVersion: "1.2",
MaxVersion: "1.3",
ForceCommonNameCheck: true,
}

var convertedAPI apidef.APIDefinition
var resultTransport TLSTransport

convertedAPI.SetDisabledFlags()
transport.ExtractTo(&convertedAPI)

assert.Equal(t, transport.InsecureSkipVerify, convertedAPI.Proxy.Transport.SSLInsecureSkipVerify)
assert.Equal(t, uint16(tls.VersionTLS12), convertedAPI.Proxy.Transport.SSLMinVersion)
assert.Equal(t, uint16(tls.VersionTLS13), convertedAPI.Proxy.Transport.SSLMaxVersion)
assert.Equal(t, transport.ForceCommonNameCheck, convertedAPI.Proxy.Transport.SSLForceCommonNameCheck)

resultTransport.Fill(convertedAPI)

assert.Equal(t, transport, resultTransport)
})

t.Run("emmpty tls settings", func(t *testing.T) {
var emptyTlsTransport TLSTransport
var convertedAPI apidef.APIDefinition
var resultTransport TLSTransport

convertedAPI.SetDisabledFlags()
emptyTlsTransport.ExtractTo(&convertedAPI)
resultTransport.Fill(convertedAPI)

assert.Equal(t, emptyTlsTransport, resultTransport)
})

t.Run("proxy settings", func(t *testing.T) {
proxyTransport := Proxy{
URL: "proxy-url",
}
var convertedAPI apidef.APIDefinition
var resultProxy Proxy

convertedAPI.SetDisabledFlags()
proxyTransport.Fill(convertedAPI)
assert.Equal(t, proxyTransport, resultProxy)
})
}

Expand Down

0 comments on commit 6f6eb36

Please sign in to comment.