forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_norace_test.go
182 lines (160 loc) · 4.95 KB
/
server_norace_test.go
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//go:build !race
// +build !race
package server
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/argoproj/argo-cd/v2/common"
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
applicationpkg "github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
"github.com/argoproj/argo-cd/v2/test"
)
func TestUserAgent(t *testing.T) {
// !race:
// A data race in go-client's `shared_informer.go`, between `sharedProcessor.run(...)` and itself. Based on
// the data race, it APPEARS to be intentional, but in any case it's nothing we are doing in Argo CD
// that is causing this issue.
s, closer := fakeServer()
defer closer()
lns, err := s.Listen()
assert.NoError(t, err)
cancelInformer := test.StartInformer(s.projInformer)
defer cancelInformer()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Init(ctx)
go s.Run(ctx, lns)
defer func() { time.Sleep(3 * time.Second) }()
type testData struct {
userAgent string
errorMsg string
}
currentVersionBytes, err := os.ReadFile("../VERSION")
assert.NoError(t, err)
currentVersion := strings.TrimSpace(string(currentVersionBytes))
var tests = []testData{
{
// Reject out-of-date user-agent
userAgent: fmt.Sprintf("%s/0.10.0", common.ArgoCDUserAgentName),
errorMsg: "unsatisfied client version constraint",
},
{
// Accept up-to-date user-agent
userAgent: fmt.Sprintf("%s/%s", common.ArgoCDUserAgentName, currentVersion),
},
{
// Accept up-to-date pre-release user-agent
userAgent: fmt.Sprintf("%s/%s-rc1", common.ArgoCDUserAgentName, currentVersion),
},
{
// Permit custom clients
userAgent: "foo/1.2.3",
},
}
for _, test := range tests {
opts := apiclient.ClientOptions{
ServerAddr: fmt.Sprintf("localhost:%d", s.ListenPort),
PlainText: true,
UserAgent: test.userAgent,
}
clnt, err := apiclient.NewClient(&opts)
assert.NoError(t, err)
conn, appClnt := clnt.NewApplicationClientOrDie()
_, err = appClnt.List(ctx, &applicationpkg.ApplicationQuery{})
if test.errorMsg != "" {
assert.Error(t, err)
assert.Regexp(t, test.errorMsg, err.Error())
} else {
assert.NoError(t, err)
}
_ = conn.Close()
}
}
func Test_StaticHeaders(t *testing.T) {
// !race:
// Same as TestUserAgent
// Test default policy "sameorigin" and "frame-ancestors 'self';"
{
s, closer := fakeServer()
defer closer()
lns, err := s.Listen()
assert.NoError(t, err)
cancelInformer := test.StartInformer(s.projInformer)
defer cancelInformer()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Init(ctx)
go s.Run(ctx, lns)
defer func() { time.Sleep(3 * time.Second) }()
// Allow server startup
time.Sleep(1 * time.Second)
client := http.Client{}
url := fmt.Sprintf("http://127.0.0.1:%d/test.html", s.ListenPort)
req, err := http.NewRequest("GET", url, nil)
assert.NoError(t, err)
resp, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, "sameorigin", resp.Header.Get("X-Frame-Options"))
assert.Equal(t, "frame-ancestors 'self';", resp.Header.Get("Content-Security-Policy"))
}
// Test custom policy for X-Frame-Options and Content-Security-Policy
{
s, closer := fakeServer()
defer closer()
s.XFrameOptions = "deny"
s.ContentSecurityPolicy = "frame-ancestors 'none';"
cancelInformer := test.StartInformer(s.projInformer)
defer cancelInformer()
lns, err := s.Listen()
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Init(ctx)
go s.Run(ctx, lns)
defer func() { time.Sleep(3 * time.Second) }()
// Allow server startup
time.Sleep(1 * time.Second)
client := http.Client{}
url := fmt.Sprintf("http://127.0.0.1:%d/test.html", s.ListenPort)
req, err := http.NewRequest("GET", url, nil)
assert.NoError(t, err)
resp, err := client.Do(req)
assert.NoError(t, err)
assert.Equal(t, "deny", resp.Header.Get("X-Frame-Options"))
assert.Equal(t, "frame-ancestors 'none';", resp.Header.Get("Content-Security-Policy"))
}
// Test disabled X-Frame-Options and Content-Security-Policy
{
s, closer := fakeServer()
defer closer()
s.XFrameOptions = ""
s.ContentSecurityPolicy = ""
cancelInformer := test.StartInformer(s.projInformer)
defer cancelInformer()
lns, err := s.Listen()
assert.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s.Init(ctx)
go s.Run(ctx, lns)
defer func() { time.Sleep(3 * time.Second) }()
err = test.WaitForPortListen(fmt.Sprintf("127.0.0.1:%d", s.ListenPort), 10*time.Second)
assert.NoError(t, err)
// Allow server startup
time.Sleep(1 * time.Second)
client := http.Client{}
url := fmt.Sprintf("http://127.0.0.1:%d/test.html", s.ListenPort)
req, err := http.NewRequest("GET", url, nil)
assert.NoError(t, err)
resp, err := client.Do(req)
assert.NoError(t, err)
assert.Empty(t, resp.Header.Get("X-Frame-Options"))
assert.Empty(t, resp.Header.Get("Content-Security-Policy"))
}
}