Skip to content

Commit 6e46737

Browse files
authored
Merge pull request #195 from Templum/develop
🔀 Preperations for next bugfix release
2 parents c7ee543 + 144a9b9 commit 6e46737

File tree

9 files changed

+257
-57
lines changed

9 files changed

+257
-57
lines changed

.github/workflows/docker.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ jobs:
3939
run: echo ${{ steps.buildx.outputs.platforms }}
4040

4141
- name: Login to Docker Hub
42-
uses: docker/login-action@v1.10.0
42+
uses: docker/login-action@v1.12.0
4343
with:
4444
username: ${{ secrets.DOCKERHUB_USERNAME }}
4545
password: ${{ secrets.DOCKERHUB_TOKEN }}
4646

4747
- name: Login to GitHub Container Registry
48-
uses: docker/login-action@v1.10.0
48+
uses: docker/login-action@v1.12.0
4949
with:
5050
registry: ghcr.io
5151
username: ${{ github.repository_owner }}

Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.17.3-alpine as base_builder
1+
FROM golang:1.17.5-alpine as base_builder
22

33
RUN apk --no-cache add ca-certificates git
44

@@ -19,7 +19,7 @@ RUN VERSION=$(git describe --all --exact-match $(git rev-parse HEAD) | grep tags
1919
-X github.com/Templum/rabbitmq-connector/pkg/version.GitCommit=${GIT_COMMIT}" \
2020
-a -installsuffix cgo -o rmq-connector .
2121

22-
FROM alpine:3.14.3
22+
FROM alpine:3.15.0
2323

2424
RUN addgroup -S app \
2525
&& adduser -S -g app app \

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ TLS Config:
3737

3838
* `TLS_ENABLED`: Set this to `true` if your RabbitMQ requires a TLS connection. Default to `false` if not set.
3939
* `TLS_CA_CERT_PATH`: Path to your CA Cert, make sure golang process is allowed to access it.
40-
* `TLS_CLIENT_CERT_PATH`: Path to Client Cert, make sure golang process is allowed to access it.
41-
* `TLS_CLIENT_KEY_PATH`: Path to Client Key, make sure golang process is allowed to access it.
40+
* `TLS_SERVER_CERT_PATH`: Path to Client Cert, make sure golang process is allowed to access it.
41+
* `TLS_SERVER_KEY_PATH`: Path to Client Key, make sure golang process is allowed to access it.
4242

4343
> Make sure if TLS is enabled, the provided `RMQ_HOST` matches the common name from the certificate. Otherwise the connection will yield a error
4444
@@ -47,8 +47,8 @@ RabbitMQ Related:
4747
* `RMQ_HOST`: Hostname/ip of Rabbit MQ
4848
* `RMQ_PORT`: Port of Rabbit MQ
4949
* `RMQ_VHOST`: Used to specify the vhost for Rabbit MQ, will default to `/`
50-
* `RMQ_USER`: Defaults to `guest`
51-
* `RMQ_PASS`: Defaults to `pass`
50+
* `RMQ_USER`: Defaults to "", if user and pass are both "" than no credentials will be used for connecting
51+
* `RMQ_PASS`: Defaults to "", if user and pass are both "" than no credentials will be used for connecting
5252
* `PATH_TO_TOPOLOGY`: Path to the yaml describing the topology, has _no_ default and is *required*
5353

5454
### Topology Configuration

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ require (
55
github.com/openfaas/connector-sdk v0.0.0-20201220114541-89f0ffcc5448
66
github.com/openfaas/faas-provider v0.18.6
77
github.com/pkg/errors v0.9.1
8-
github.com/spf13/afero v1.6.0
8+
github.com/spf13/afero v1.7.1
99
github.com/streadway/amqp v1.0.0
1010
github.com/stretchr/testify v1.7.0
11-
github.com/testcontainers/testcontainers-go v0.11.1
11+
github.com/testcontainers/testcontainers-go v0.12.0
1212
github.com/valyala/fasthttp v1.31.0
1313
go.uber.org/automaxprocs v1.4.0
1414
gopkg.in/yaml.v2 v2.4.0

go.sum

+166-22
Large diffs are not rendered by default.

pkg/config/config.go

+15-24
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ func NewConfig(fs afero.Fs) (*Controller, error) {
5151
var tlsConfig *tls.Config = nil
5252

5353
if readFromEnv(envUseTLS, "false") == "true" {
54-
rabbitURL, err = getRabbitMQConnectionURL(true)
55-
sanitizedURL = getSanitizedRabbitMQURL(true)
54+
rabbitURL, sanitizedURL, err = getRabbitMQConnectionURL(true)
5655

5756
if cfg, confErr := generateTlsConfig(fs); confErr == nil {
5857
tlsConfig = cfg
@@ -61,8 +60,7 @@ func NewConfig(fs afero.Fs) (*Controller, error) {
6160
}
6261

6362
} else {
64-
rabbitURL, err = getRabbitMQConnectionURL(false)
65-
sanitizedURL = getSanitizedRabbitMQURL(false)
63+
rabbitURL, sanitizedURL, err = getRabbitMQConnectionURL(false)
6664
}
6765

6866
if err != nil {
@@ -177,42 +175,35 @@ func generateTlsConfig(fs afero.Fs) (*tls.Config, error) {
177175
return cfg, nil
178176
}
179177

180-
func getRabbitMQConnectionURL(isTls bool) (string, error) {
181-
user := readFromEnv(envRabbitUser, "user")
182-
pass := readFromEnv(envRabbitPass, "pass")
178+
// getRabbitMQConnectionURL returns the fully build url and the sanitized version for usage in logging
179+
func getRabbitMQConnectionURL(isTls bool) (string, string, error) {
180+
user := readFromEnv(envRabbitUser, "")
181+
pass := readFromEnv(envRabbitPass, "")
183182
host := readFromEnv(envRabbitHost, "localhost")
184183
port := readFromEnv(envRabbitPort, "5672")
185184
vhost := readFromEnv(envRabbitVHost, "")
185+
protocol := "amqp"
186+
if isTls {
187+
protocol = "amqps"
188+
}
186189

187190
parsedPort, err := strconv.Atoi(port)
188191

189192
if err != nil {
190193
message := fmt.Sprintf("Provided port %s is not a valid port", port)
191-
return "", errors.New(message)
194+
return "", "", errors.New(message)
192195
}
193196

194197
if parsedPort <= 0 || parsedPort > 65535 {
195198
message := fmt.Sprintf("Provided port %s is outside of the allowed port range", port)
196-
return "", errors.New(message)
197-
}
198-
199-
if isTls {
200-
return fmt.Sprintf("amqps://%s:%s/%s", host, port, vhost), nil
199+
return "", "", errors.New(message)
201200
}
202201

203-
return fmt.Sprintf("amqp://%s:%s@%s:%s/%s", user, pass, host, port, vhost), nil
204-
}
205-
206-
func getSanitizedRabbitMQURL(isTls bool) string {
207-
host := readFromEnv(envRabbitHost, "localhost")
208-
port := readFromEnv(envRabbitPort, "5672")
209-
vhost := readFromEnv(envRabbitVHost, "")
210-
211-
if isTls {
212-
return fmt.Sprintf("amqps://%s:%s/%s", host, port, vhost)
202+
if user == "" && pass == "" {
203+
return fmt.Sprintf("%s://%s:%s/%s", protocol, host, port, vhost), fmt.Sprintf("%s://%s:%s/%s", protocol, host, port, vhost), nil
213204
}
214205

215-
return fmt.Sprintf("amqp://%s:%s/%s", host, port, vhost)
206+
return fmt.Sprintf("%s://%s:%s@%s:%s/%s", protocol, user, pass, host, port, vhost), fmt.Sprintf("%s://%s:%s/%s", protocol, host, port, vhost), nil
216207
}
217208

218209
func getTopology(fs afero.Fs) (internal.Topology, error) {

pkg/config/config_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ data:
244244
assert.Nil(t, err, "Should not throw")
245245
assert.Nil(t, config.TLSConfig, "Should not have a TLS config")
246246
assert.Equal(t, config.GatewayURL, "http://gateway:8080", "Expected default value")
247-
assert.Equal(t, config.RabbitConnectionURL, "amqp://user:pass@localhost:5672/", "Expected default value")
247+
assert.Equal(t, config.RabbitConnectionURL, "amqp://localhost:5672/", "Expected default value")
248248
assert.NotContains(t, config.RabbitSanitizedURL, "user:pass", "Expected credentials not to be present")
249249
assert.Equal(t, config.RabbitSanitizedURL, "amqp://localhost:5672/", "Expected default value")
250250
assert.Equal(t, config.TopicRefreshTime, 30*time.Second, "Expected default value")

pkg/openfaas/client.go

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (c *Client) InvokeSync(ctx context.Context, name string, invocation *intern
7777
req.SetBody(nil)
7878
}
7979

80+
req.Header.SetMethod(fasthttp.MethodPost)
8081
req.Header.Set("Content-Type", invocation.ContentType)
8182
req.Header.Set("Content-Encoding", invocation.ContentEncoding)
8283
req.Header.SetUserAgent("OpenFaaS - Rabbit MQ Connector")
@@ -118,6 +119,7 @@ func (c *Client) InvokeAsync(ctx context.Context, name string, invocation *inter
118119
req.SetBody(nil)
119120
}
120121

122+
req.Header.SetMethod(fasthttp.MethodPost)
121123
req.Header.Set("Content-Type", invocation.ContentType)
122124
req.Header.Set("Content-Encoding", invocation.ContentEncoding)
123125
req.Header.SetUserAgent("OpenFaaS - Rabbit MQ Connector")
@@ -154,6 +156,7 @@ func (c *Client) HasNamespaceSupport(ctx context.Context) (bool, error) {
154156

155157
req.SetRequestURI(getNamespaces)
156158

159+
req.Header.SetMethod(fasthttp.MethodGet)
157160
req.Header.SetUserAgent("OpenFaaS - Rabbit MQ Connector")
158161
if c.credentials != nil {
159162
credentials := c.credentials.User + ":" + c.credentials.Password
@@ -190,6 +193,7 @@ func (c *Client) GetNamespaces(ctx context.Context) ([]string, error) {
190193

191194
req.SetRequestURI(getNamespaces)
192195

196+
req.Header.SetMethod(fasthttp.MethodGet)
193197
req.Header.SetUserAgent("OpenFaaS - Rabbit MQ Connector")
194198
if c.credentials != nil {
195199
credentials := c.credentials.User + ":" + c.credentials.Password
@@ -226,6 +230,7 @@ func (c *Client) GetFunctions(ctx context.Context, namespace string) ([]types.Fu
226230

227231
req.SetRequestURI(getFunctions)
228232

233+
req.Header.SetMethod(fasthttp.MethodGet)
229234
req.Header.SetUserAgent("OpenFaaS - Rabbit MQ Connector")
230235
if c.credentials != nil {
231236
credentials := c.credentials.User + ":" + c.credentials.Password

pkg/openfaas/client_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func TestClient_InvokeSync(t *testing.T) {
4343
return
4444
}
4545

46+
if r.Method != fasthttp.MethodPost {
47+
w.WriteHeader(400)
48+
fmt.Fprint(w, "Method not supported")
49+
return
50+
}
51+
4652
switch r.URL.Path {
4753
case "/function/exists":
4854
w.WriteHeader(200)
@@ -127,6 +133,12 @@ func TestClient_InvokeAsync(t *testing.T) {
127133
return
128134
}
129135

136+
if r.Method != fasthttp.MethodPost {
137+
w.WriteHeader(400)
138+
fmt.Fprint(w, "Method not supported")
139+
return
140+
}
141+
130142
switch r.URL.Path {
131143
case "/async-function/exists":
132144
w.WriteHeader(202)
@@ -210,6 +222,12 @@ func TestClient_HasNamespaceSupport(t *testing.T) {
210222
return
211223
}
212224

225+
if r.Method != fasthttp.MethodGet {
226+
w.WriteHeader(400)
227+
fmt.Fprint(w, "Method not supported")
228+
return
229+
}
230+
213231
namespaces := []string{"one", "two"}
214232
out, _ := json.Marshal(namespaces)
215233

@@ -324,6 +342,12 @@ func TestClient_GetFunctions(t *testing.T) {
324342
return
325343
}
326344

345+
if r.Method != fasthttp.MethodGet {
346+
w.WriteHeader(400)
347+
fmt.Fprint(w, "Method not supported")
348+
return
349+
}
350+
327351
namespace := r.URL.Query().Get("namespace")
328352
if len(namespace) > 0 {
329353
if namespace == "special" {
@@ -410,6 +434,12 @@ func TestClient_GetNamespaces(t *testing.T) {
410434
return
411435
}
412436

437+
if r.Method != fasthttp.MethodGet {
438+
w.WriteHeader(400)
439+
fmt.Fprint(w, "Method not supported")
440+
return
441+
}
442+
413443
w.WriteHeader(200)
414444
out, _ := json.Marshal(namespaces)
415445
_, _ = w.Write(out)
@@ -450,3 +480,33 @@ func TestClient_GetNamespaces(t *testing.T) {
450480
assert.Error(t, err, "OpenFaaS Credentials are invalid", "Did receive unexpected error")
451481
})
452482
}
483+
484+
func TestClient_Edge(t *testing.T) {
485+
openfaasClient := NewClient(CreateClient(nil), nil, "ftp://localhost/")
486+
487+
payload := types2.OpenFaaSInvocation{
488+
Topic: "",
489+
Message: nil,
490+
ContentEncoding: "gzip",
491+
ContentType: "text/plain",
492+
}
493+
494+
t.Run("Should throw error if invalid base URL is provided", func(t *testing.T) {
495+
var err error
496+
497+
_, err = openfaasClient.InvokeSync(context.Background(), "exists", &payload)
498+
assert.Error(t, err, "unsupported protocol ftp. http and https are supported", "Did receive unexpected error")
499+
500+
_, err = openfaasClient.InvokeAsync(context.Background(), "exists", &payload)
501+
assert.Error(t, err, "unsupported protocol ftp. http and https are supported", "Did receive unexpected error")
502+
503+
_, err = openfaasClient.GetNamespaces(context.Background())
504+
assert.Error(t, err, "unsupported protocol ftp. http and https are supported", "Did receive unexpected error")
505+
506+
_, err = openfaasClient.GetFunctions(context.Background(), "")
507+
assert.Error(t, err, "unsupported protocol ftp. http and https are supported", "Did receive unexpected error")
508+
509+
_, err = openfaasClient.HasNamespaceSupport(context.Background())
510+
assert.Error(t, err, "unsupported protocol ftp. http and https are supported", "Did receive unexpected error")
511+
})
512+
}

0 commit comments

Comments
 (0)