This repository was archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_test.go
More file actions
166 lines (146 loc) · 4.47 KB
/
integration_test.go
File metadata and controls
166 lines (146 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package auth_test
import (
"context"
"fmt"
"net"
"testing"
"time"
"github.com/containerssh/auth"
"github.com/containerssh/geoip"
"github.com/containerssh/http"
"github.com/containerssh/log"
"github.com/containerssh/metrics"
"github.com/containerssh/service"
"github.com/stretchr/testify/assert"
)
type handler struct {
}
func (h *handler) OnPassword(
username string,
password []byte,
remoteAddress string,
connectionID string,
) (bool, error) {
if remoteAddress != "127.0.0.1" {
return false, fmt.Errorf("invalid IP: %s", remoteAddress)
}
if connectionID != "0123456789ABCDEF" {
return false, fmt.Errorf("invalid connection ID: %s", connectionID)
}
if username == "foo" && string(password) == "bar" {
return true, nil
}
if username == "crash" {
// Simulate a database failure
return false, fmt.Errorf("database error")
}
return false, nil
}
func (h *handler) OnPubKey(username string, publicKey string, remoteAddress string, connectionID string) (bool, error) {
if remoteAddress != "127.0.0.1" {
return false, fmt.Errorf("invalid IP: %s", remoteAddress)
}
if connectionID != "0123456789ABCDEF" {
return false, fmt.Errorf("invalid connection ID: %s", connectionID)
}
if username == "foo" && publicKey == "ssh-rsa asdf" {
return true, nil
}
if username == "crash" {
// Simulate a database failure
return false, fmt.Errorf("database error")
}
return false, nil
}
func TestAuth(t *testing.T) {
logger := log.NewTestLogger(t)
logger.Info(
log.NewMessage(
"TEST",
"FYI: errors during this test are expected as we test against error cases.",
),
)
for name, subpath := range map[string]string{"empty": "", "auth": "/auth"} {
t.Run(fmt.Sprintf("subpath_%s", name), func(t *testing.T) {
client, lifecycle, metricsCollector, err := initializeAuth(logger, subpath)
if err != nil {
assert.Fail(t, "failed to initialize auth", err)
return
}
defer lifecycle.Stop(context.Background())
success, err := client.Password("foo", []byte("bar"), "0123456789ABCDEF", net.ParseIP("127.0.0.1"))
assert.Equal(t, nil, err)
assert.Equal(t, true, success)
assert.Equal(t, float64(1), metricsCollector.GetMetric(auth.MetricNameAuthBackendRequests)[0].Value)
assert.Equal(t, float64(1), metricsCollector.GetMetric(auth.MetricNameAuthSuccess)[0].Value)
success, err = client.Password("foo", []byte("baz"), "0123456789ABCDEF", net.ParseIP("127.0.0.1"))
assert.Equal(t, nil, err)
assert.Equal(t, false, success)
assert.Equal(t, float64(1), metricsCollector.GetMetric(auth.MetricNameAuthFailure)[0].Value)
success, err = client.Password("crash", []byte("baz"), "0123456789ABCDEF", net.ParseIP("127.0.0.1"))
assert.NotEqual(t, nil, err)
assert.Equal(t, false, success)
assert.Equal(t, float64(1), metricsCollector.GetMetric(auth.MetricNameAuthBackendFailure)[0].Value)
success, err = client.PubKey("foo", "ssh-rsa asdf", "0123456789ABCDEF", net.ParseIP("127.0.0.1"))
assert.Equal(t, nil, err)
assert.Equal(t, true, success)
success, err = client.PubKey("foo", "ssh-rsa asdx", "0123456789ABCDEF", net.ParseIP("127.0.0.1"))
assert.Equal(t, nil, err)
assert.Equal(t, false, success)
success, err = client.PubKey("crash", "ssh-rsa asdx", "0123456789ABCDEF", net.ParseIP("127.0.0.1"))
assert.NotEqual(t, nil, err)
assert.Equal(t, false, success)
})
}
}
func initializeAuth(logger log.Logger, subpath string) (auth.Client, service.Lifecycle, metrics.Collector, error) {
ready := make(chan bool, 1)
errors := make(chan error)
server, err := auth.NewServer(
http.ServerConfiguration{
Listen: "127.0.0.1:8080",
},
&handler{},
logger,
)
if err != nil {
return nil, nil, nil, err
}
geoipProvider, err := geoip.New(geoip.Config{
Provider: geoip.DummyProvider,
})
if err != nil {
return nil, nil, nil, err
}
metricsCollector := metrics.New(geoipProvider)
client, err := auth.NewHttpAuthClient(
auth.ClientConfig{
ClientConfiguration: http.ClientConfiguration{
URL: fmt.Sprintf("http://127.0.0.1:8080%s", subpath),
Timeout: 2 * time.Second,
},
Password: true,
PubKey: true,
AuthTimeout: 2 * time.Second,
},
logger,
metricsCollector,
)
if err != nil {
return nil, nil, nil, err
}
lifecycle := service.NewLifecycle(server)
lifecycle.OnRunning(
func(_ service.Service, _ service.Lifecycle) {
ready <- true
},
)
go func() {
if err := lifecycle.Run(); err != nil {
errors <- err
}
close(errors)
}()
<-ready
return client, lifecycle, metricsCollector, nil
}