@@ -11,7 +11,7 @@ import (
11
11
"github.com/aws/aws-sdk-go/service/ec2"
12
12
)
13
13
14
- type AwsInstance struct {
14
+ type awsInstance struct {
15
15
InstanceID string
16
16
InstanceType string
17
17
PublicIPAddress string
@@ -27,10 +27,11 @@ func (a AWSWeb) TokenManager(w http.ResponseWriter, r *http.Request) {
27
27
// get the swarm manager token, if they are a manager node,
28
28
// and are not already in the swarm. Block otherwise
29
29
RequestInfo (r )
30
- found := alreadyInSwarm (r )
31
- isManager := isManagerNode (r )
30
+ ip := RequestIP (r )
31
+ inSwarm := alreadyInSwarm (ip )
32
+ isManager := isManagerNode (ip )
32
33
33
- if found || ! isManager {
34
+ if inSwarm || ! isManager {
34
35
// they are either already in the swarm, or they are not a manager
35
36
w .WriteHeader (http .StatusForbidden )
36
37
fmt .Fprintln (w , "Access Denied" )
@@ -43,6 +44,7 @@ func (a AWSWeb) TokenManager(w http.ResponseWriter, r *http.Request) {
43
44
if err != nil {
44
45
w .WriteHeader (http .StatusInternalServerError )
45
46
fmt .Fprintf (w , "%v" , err )
47
+ return
46
48
}
47
49
48
50
fmt .Fprintf (w , swarm .JoinTokens .Manager )
@@ -53,10 +55,11 @@ func (a AWSWeb) TokenWorker(w http.ResponseWriter, r *http.Request) {
53
55
// and are not already in the swarm. block otherwise
54
56
RequestInfo (r )
55
57
56
- found := alreadyInSwarm (r )
57
- isWorker := isWorkerNode (r )
58
+ ip := RequestIP (r )
59
+ inSwarm := alreadyInSwarm (ip )
60
+ isWorker := isWorkerNode (ip )
58
61
59
- if found || ! isWorker {
62
+ if inSwarm || ! isWorker {
60
63
// they are either already in the swarm, or they are not a worker
61
64
w .WriteHeader (http .StatusForbidden )
62
65
fmt .Fprintln (w , "Access Denied" )
@@ -69,40 +72,36 @@ func (a AWSWeb) TokenWorker(w http.ResponseWriter, r *http.Request) {
69
72
if err != nil {
70
73
w .WriteHeader (http .StatusInternalServerError )
71
74
fmt .Fprintf (w , "%v" , err )
75
+ return
72
76
}
73
77
74
78
fmt .Fprintf (w , swarm .JoinTokens .Worker )
75
79
}
76
80
77
- func alreadyInSwarm (r * http. Request ) bool {
81
+ func alreadyInSwarm (ip string ) bool {
78
82
// Is the node making the request, already in the swarm.
79
- ip := RequestIP (r )
80
83
nodes := SwarmNodes ()
81
84
for _ , node := range nodes {
82
- nodeIP := convertAWSHostToIP (node .Description .Hostname )
85
+ nodeIP := ConvertAWSHostToIP (node .Description .Hostname )
83
86
if ip == nodeIP {
84
87
return true
85
88
}
86
89
}
87
90
return false
88
91
}
89
92
90
- func isManagerNode (r * http. Request ) bool {
93
+ func isManagerNode (ip string ) bool {
91
94
// Is the node making the request a manager node
92
- ip := RequestIP (r )
93
- instances := awsManagers ()
94
- for _ , instance := range instances {
95
- if ip == instance .PrivateIPAddress {
96
- return true
97
- }
98
- }
99
- return false
95
+ return isNodeInList (ip , awsManagers ())
100
96
}
101
97
102
- func isWorkerNode (r * http. Request ) bool {
98
+ func isWorkerNode (ip string ) bool {
103
99
// Is the node making the request a worker node
104
- ip := RequestIP (r )
105
- instances := awsWorkers ()
100
+ return isNodeInList (ip , awsWorkers ())
101
+ }
102
+
103
+ func isNodeInList (ip string , instances []awsInstance ) bool {
104
+ // given an IP, find out if it is in the instance list.
106
105
for _ , instance := range instances {
107
106
if ip == instance .PrivateIPAddress {
108
107
return true
@@ -111,9 +110,8 @@ func isWorkerNode(r *http.Request) bool {
111
110
return false
112
111
}
113
112
114
- func awsWorkers () []AwsInstance {
113
+ func awsWorkers () []awsInstance {
115
114
// get the instances from AWS worker security group
116
-
117
115
customFilter := []* ec2.Filter {
118
116
& ec2.Filter {
119
117
Name : aws .String ("tag:swarm-node-type" ),
@@ -130,9 +128,8 @@ func awsWorkers() []AwsInstance {
130
128
return awsInstances (customFilter )
131
129
}
132
130
133
- func awsManagers () []AwsInstance {
131
+ func awsManagers () []awsInstance {
134
132
// get the instances from AWS Manager security group
135
-
136
133
customFilter := []* ec2.Filter {
137
134
& ec2.Filter {
138
135
Name : aws .String ("tag:swarm-node-type" ),
@@ -149,9 +146,8 @@ func awsManagers() []AwsInstance {
149
146
return awsInstances (customFilter )
150
147
}
151
148
152
- func awsInstances (customFilters []* ec2.Filter ) []AwsInstance {
149
+ func awsInstances (customFilters []* ec2.Filter ) []awsInstance {
153
150
// get the instances from AWS, takes a filter to limit the results.
154
-
155
151
client := ec2 .New (session .New (& aws.Config {}))
156
152
157
153
// Only grab instances that are running or just started
@@ -177,24 +173,24 @@ func awsInstances(customFilters []*ec2.Filter) []AwsInstance {
177
173
fmt .Println (err .Error ())
178
174
}
179
175
180
- var instances []AwsInstance
176
+ var instances []awsInstance
181
177
for _ , reservation := range result .Reservations {
182
178
for _ , instance := range reservation .Instances {
183
- awsInstance := AwsInstance {
179
+ aInstance := awsInstance {
184
180
InstanceID : * instance .InstanceId ,
185
181
InstanceType : * instance .InstanceType ,
186
182
PublicIPAddress : * instance .PublicIpAddress ,
187
183
PrivateIPAddress : * instance .PrivateIpAddress ,
188
184
InstanceState : * instance .State .Name ,
189
185
InstanceAZ : * instance .Placement .AvailabilityZone ,
190
186
}
191
- instances = append (instances , awsInstance )
187
+ instances = append (instances , aInstance )
192
188
}
193
189
}
194
190
return instances
195
191
}
196
192
197
- func convertAWSHostToIP (hostStr string ) string {
193
+ func ConvertAWSHostToIP (hostStr string ) string {
198
194
// This is risky, this assumes the following formation for hosts in swarm node ls
199
195
// ip-10-0-3-149.ec2.internal
200
196
// there was one use case when someone had an old account, and their hostnames were not
0 commit comments