-
Notifications
You must be signed in to change notification settings - Fork 21
/
httpservices.go
178 lines (163 loc) · 5.18 KB
/
httpservices.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
// Copyright (c) 2016 by António Meireles <antonio.meireles@reformi.st>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package server
import (
"encoding/json"
"io/ioutil"
"net"
"net/http"
"strconv"
"strings"
"golang.org/x/net/context"
"github.com/TheNewNormal/corectl/components/host/session"
"github.com/TheNewNormal/corectl/components/target/coreos"
"github.com/coreos/fuze/config"
"github.com/coreos/go-systemd/unit"
"github.com/gorilla/mux"
)
var httpServices = mux.NewRouter()
func httpServiceSetup() {
httpServices.HandleFunc("/{uuid}/ignition", httpInstanceIgnitionConfig)
httpServices.HandleFunc("/{uuid}/cloud-config", httpInstanceCloudConfig)
httpServices.HandleFunc("/{uuid}/homedir", httpInstanceHomedirMountConfig)
httpServices.HandleFunc("/{uuid}/ping", httpInstanceCallback)
httpServices.HandleFunc("/{uuid}/NotIsolated",
httpInstanceExternalConnectivity)
}
func remoteIP(s string) string {
return strings.Split(s, ":")[0]
}
func isLoopback(s string) bool {
if strings.HasPrefix(s, "127.") {
return true
}
return false
}
func httpError(w http.ResponseWriter, status int) {
w.WriteHeader(status)
w.Write(nil)
}
func acceptableRequest(r *http.Request, w http.ResponseWriter) bool {
var (
vmNetwork = net.IPNet{
IP: net.ParseIP(session.Caller.Address),
Mask: net.IPMask(net.ParseIP(session.Caller.Mask).To4()),
}
addr = remoteIP(r.RemoteAddr)
status = http.StatusUnauthorized
)
if isLoopback(addr) || vmNetwork.Contains(net.ParseIP(addr)) {
if _, ok := Daemon.Active[mux.Vars(r)["uuid"]]; ok {
return true
}
status = http.StatusNotFound
}
httpError(w, status)
return false
}
func httpInstanceCloudConfig(w http.ResponseWriter, r *http.Request) {
if acceptableRequest(r, w) {
vm := Daemon.Active[mux.Vars(r)["uuid"]]
if vm.CloudConfig == "" || vm.CClocation != Local {
httpError(w, http.StatusPreconditionFailed)
} else if t, err := ioutil.ReadFile(vm.CloudConfig); err != nil {
httpError(w, http.StatusInternalServerError)
} else {
w.Write(t)
}
}
}
func reverseDomain(ip string) string {
const IPv4prefix = "in-addr.arpa"
var d string
addr := net.ParseIP(ip)
ip4addr := addr.To4()
domain := make([]byte, 0, len(ip4addr)*4+len(IPv4prefix))
for x := len(ip4addr) - 1; x >= 0; x-- {
domain = strconv.AppendUint(domain, uint64(ip4addr[x]), 10)
domain = append(domain, '.')
}
domain = append(domain, IPv4prefix...)
y := strings.Split(string(domain), ".")
for x := len(y) - 1; x >= 0; x-- {
d += y[x] + "/"
}
d = strings.TrimSuffix(d, "/")
return string(d)
}
func httpInstanceIgnitionConfig(w http.ResponseWriter, r *http.Request) {
if acceptableRequest(r, w) {
vm := Daemon.Active[mux.Vars(r)["uuid"]]
mods := strings.NewReplacer(
"__vm.InternalSSHkey__", vm.InternalSSHkey,
"__vm.Name__", vm.Name,
"__vm.Gateway__", session.Caller.Network.Address,
"__corectl.Version__", Daemon.Meta.Version)
if cfgIn, err := config.ParseAsV2_0_0(
[]byte(mods.Replace(coreos.CoreOSIgnitionTmpl))); err != nil {
httpError(w, http.StatusInternalServerError)
} else if i, err := json.MarshalIndent(&cfgIn, "", " "); err != nil {
httpError(w, http.StatusInternalServerError)
} else {
w.Write([]byte(append(i, '\n')))
if !isLoopback(remoteIP(r.RemoteAddr)) {
_, _ = Daemon.DataStore.Set(context.Background(),
"/skydns/local/coreos/"+vm.Name,
"{\"host\":\""+remoteIP(r.RemoteAddr)+"\"}", nil)
_, _ = Daemon.DataStore.Set(context.Background(),
"/skydns/"+reverseDomain(remoteIP(r.RemoteAddr)),
"{\"host\":\""+vm.Name+".coreos.local\"}", nil)
}
}
}
}
func httpInstanceHomedirMountConfig(w http.ResponseWriter, r *http.Request) {
if acceptableRequest(r, w) {
mods := strings.NewReplacer("((server))", session.Caller.Address,
"((path))", session.Caller.HomeDir,
"((path_escaped))", unit.UnitNamePathEscape(session.Caller.HomeDir))
w.Write([]byte(mods.Replace(coreos.CoreOEMsharedHomedir)))
}
}
func httpInstanceExternalConnectivity(w http.ResponseWriter, r *http.Request) {
if acceptableRequest(r, w) {
if isLoopback(remoteIP(r.RemoteAddr)) {
httpError(w, http.StatusUnauthorized)
} else {
vm := Daemon.Active[mux.Vars(r)["uuid"]]
w.Write([]byte("ok\n"))
vm.isolationCheck.Do(func() {
Daemon.Lock()
Daemon.Active[vm.UUID].NotIsolated = true
Daemon.Unlock()
})
}
}
}
func httpInstanceCallback(w http.ResponseWriter, r *http.Request) {
if acceptableRequest(r, w) {
if isLoopback(remoteIP(r.RemoteAddr)) {
httpError(w, http.StatusUnauthorized)
} else {
vm := Daemon.Active[mux.Vars(r)["uuid"]]
w.Write([]byte("ok\n"))
vm.callBack.Do(func() {
Daemon.Lock()
Daemon.Active[vm.UUID].publicIPCh <- remoteIP(r.RemoteAddr)
Daemon.Unlock()
})
}
}
}