aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 9d12c9fc39dd557f18a94b9aa8d9c573fbbc5339 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main

import (
	"bytes"
	"crypto/hmac"
	"crypto/sha256"
	"encoding/hex"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"os"
	"os/user"
	"strconv"
	"time"

	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/ec2metadata"
	"github.com/gorilla/handlers"
	"github.com/gorilla/mux"
	"github.com/sid77/drop"
	jww "github.com/spf13/jwalterweatherman"
)

var (
	DEFAULT_VALUES map[string]*string
)

func init() {
	DEFAULT_VALUES = map[string]*string{
		"domain":            StringPtr("amazonaws.com"),
		"partition":         StringPtr("aws"),
		"ami-launch-index":  StringPtr("0"),
		"ami-manifest-path": StringPtr("(unknown)"),
		"instance-action":   StringPtr("none"),
		"profile":           StringPtr("default-hvm"),
		"security-groups":   StringPtr("default"),
	}
}

func StringPtr(v string) *string {
	return &v
}

type appContext struct {
	PrivateIP         *net.IP
	Region            *string
	MacAddr           *net.HardwareAddr
	AvailabilityZone  *string
	Hostname          *string
	InstanceId        *string
	InstanceType      *string
	AccountId         *int64
	ImageId           *string
	ReservationId     *string
	InstanceProfileId *string
	RoleARN           *string
	BootstrapSecret   *string
	CredentialHandler CredentialHandler
}

func (c *appContext) FormatAZ() string {
	return fmt.Sprintf("%s%s", *c.Region, *c.AvailabilityZone)
}

func GetKeyHandler(content map[string]*string, key string) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(*content[key]))
	})
}

func AvailabilityZoneHandler(w http.ResponseWriter, r *http.Request) {
	ctx := getAppCtx(r)
	fmt.Fprintf(w, ctx.FormatAZ())
}

func IAMCredentialHandler(w http.ResponseWriter, r *http.Request) {
	ctx := getAppCtx(r)
	vars := mux.Vars(r)

	name, err := parseRoleName(*ctx.RoleARN)
	if err != nil {
		jww.ERROR.Printf("Error parsing role name in IAMCredentialHandler: %s", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	if vars["profile"] != name {
		http.NotFound(w, r)
		return
	}

	writeHTTPJson(w, <-ctx.CredentialHandler.Output(), "IAMCredentialHandler")
}

func StatusHandler(w http.ResponseWriter, r *http.Request) {
	ctx := getAppCtx(r)
	writeHTTPJson(w, ctx.CredentialHandler.InGoodState(), "IAMCredentialHandler")
}

type bootstrapInput struct {
	AccessKeyId     string
	SecretAccessKey string
	Token           string
	Signature       string
}

func validateSignature(r *bootstrapInput, key *string) bool {
	buf := bytes.Buffer{}
	// Alphabetical order matters here
	buf.WriteString(fmt.Sprintf("AccessKeyId%s", r.AccessKeyId))
	buf.WriteString(fmt.Sprintf("SecretAccessKey%s", r.SecretAccessKey))

	// Only hash token if it was presented
	if r.Token != "" {
		buf.WriteString(fmt.Sprintf("Token%s", r.Token))
	}

	mac := hmac.New(sha256.New, []byte(*key))
	mac.Write(buf.Bytes())
	expected := mac.Sum(nil)

	sig, err := hex.DecodeString(r.Signature)
	if err != nil {
		return false
	}

	return hmac.Equal(expected, sig)
}

func BootstrapCredentialHandler(w http.ResponseWriter, r *http.Request) {
	ctx := getAppCtx(r)
	d := json.NewDecoder(r.Body)
	var cred bootstrapInput

	err := d.Decode(&cred)
	if err != nil {
		jww.ERROR.Printf("Error decoding bootstrap JSON: %s", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	if !validateSignature(&cred, ctx.BootstrapSecret) {
		jww.ERROR.Printf("Invalid signature for bootstrapping credentials")
		http.Error(w, "Not allowed", http.StatusForbidden)
		return
	}

	creds := credentials.NewStaticCredentials(cred.AccessKeyId, cred.SecretAccessKey, cred.Token)
	ctx.CredentialHandler.SetBootstrapCredential(creds)
}

func IAMInfoHandler(w http.ResponseWriter, r *http.Request) {
	ctx := getAppCtx(r)

	p := &ec2metadata.EC2IAMInfo{
		Code:               "Success",
		LastUpdated:        time.Now().UTC().Round(time.Second),
		InstanceProfileArn: *ctx.RoleARN,
		InstanceProfileID:  *ctx.InstanceProfileId,
	}

	writeHTTPJson(w, p, "IAMInfoHandler")
}

func InstanceProfileListHandler(w http.ResponseWriter, r *http.Request) {
	ctx := getAppCtx(r)

	if !ctx.CredentialHandler.InGoodState() {
		jww.ERROR.Printf("Credential handler in a bad state")
		fmt.Fprintf(w, "")
		return
	}

	name, err := parseRoleName(*ctx.RoleARN)
	if err != nil {
		jww.ERROR.Printf("Error parsing role name in InstanceProfileListHandler: %s", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	fmt.Fprintf(w, name)
}

func IdentityDocumentHandler(w http.ResponseWriter, r *http.Request) {
	ctx := getAppCtx(r)

	id := &ec2metadata.EC2InstanceIdentityDocument{
		PrivateIP:          ctx.PrivateIP.String(),
		DevpayProductCodes: nil,
		AvailabilityZone:   ctx.FormatAZ(),
		Version:            "2010-08-31",
		InstanceID:         *ctx.InstanceId,
		BillingProducts:    nil,
		InstanceType:       *ctx.InstanceType,
		ImageID:            *ctx.ImageId,
		AccountID:          strconv.FormatInt(*ctx.AccountId, 10),
		Architecture:       "x86_64",
		KernelID:           "",
		RamdiskID:          "",
		PendingTime:        time.Now().Round(time.Second),
		Region:             *ctx.Region,
	}

	writeHTTPJson(w, id, "IdentityDocumentHandler")
}

func buildMetadataHandler(ctx *appContext, defaults map[string]*string) http.Handler {
	r := mux.NewRouter()

	// Static Data Handlers
	r.Handle("/latest/meta-data/services/domain", GetKeyHandler(defaults, "domain"))
	r.Handle("/latest/meta-data/services/partition", GetKeyHandler(defaults, "partition"))
	r.Handle("/latest/meta-data/ami-launch-index", GetKeyHandler(defaults, "ami-launch-index"))
	r.Handle("/latest/meta-data/ami-manifest-path", GetKeyHandler(defaults, "ami-manifest-path"))
	r.Handle("/latest/meta-data/instance-action", GetKeyHandler(defaults, "instance-action"))
	r.Handle("/latest/meta-data/profile", GetKeyHandler(defaults, "profile"))
	r.Handle("/latest/meta-data/security-groups", GetKeyHandler(defaults, "security-groups"))

	// Machine-specific Pseudo-static Handlers
	r.Handle("/latest/meta-data/mac", ContextPrintingHandler{ctx, "MacAddr"})
	r.Handle("/latest/meta-data/hostname", ContextPrintingHandler{ctx, "Hostname"})
	r.Handle("/latest/meta-data/local-hostname", ContextPrintingHandler{ctx, "Hostname"})
	r.Handle("/latest/meta-data/local-ipv4", ContextPrintingHandler{ctx, "PrivateIP"})
	r.Handle("/latest/meta-data/instance-id", ContextPrintingHandler{ctx, "InstanceId"})
	r.Handle("/latest/meta-data/instance-type", ContextPrintingHandler{ctx, "InstanceType"})
	r.Handle("/latest/meta-data/ami-id", ContextPrintingHandler{ctx, "ImageId"})
	r.Handle("/latest/meta-data/reservation-id", ContextPrintingHandler{ctx, "ReservationId"})

	// Context-specific Handlers
	r.Handle("/latest/meta-data/placement/availability-zone", ContextAwareHandler{ctx, AvailabilityZoneHandler})
	r.Handle("/latest/dynamic/instance-identity/document", ContextAwareHandler{ctx, IdentityDocumentHandler})

	// IAM Credential Handlers
	r.Handle("/latest/meta-data/iam/info", ContextAwareHandler{ctx, IAMInfoHandler})
	r.Handle("/latest/meta-data/iam/security-credentials/", ContextAwareHandler{ctx, InstanceProfileListHandler})
	r.Handle("/latest/meta-data/iam/security-credentials/{profile}", ContextAwareHandler{ctx, IAMCredentialHandler})

	return handlers.LoggingHandler(os.Stdout, SecurityHandler{r})
}

func buildAdminHandler(ctx *appContext) http.Handler {
	r := mux.NewRouter()

	r.Handle("/bootstrap/creds", ContextAwareHandler{ctx, BootstrapCredentialHandler}).Methods("POST")
	r.Handle("/status", ContextAwareHandler{ctx, StatusHandler}).Methods("GET")

	return handlers.LoggingHandler(os.Stdout, r)
}

type UserArgs struct {
	regionAZ         string
	InstanceType     string
	Region           string
	AvailabilityZone string
	RoleARN          string
	User             string
	AccountNumber    int64
	BootstrapSecret  string
}

func parseArgs() (*UserArgs, error) {
	a := &UserArgs{}

	flag.StringVar(&a.regionAZ, "region", "us-west-2a", "region and availability zone")
	flag.StringVar(&a.InstanceType, "instance", "t2.micro", "instance type")
	flag.StringVar(&a.RoleARN, "arn", "", "ARN of role to assume")
	flag.StringVar(&a.User, "user", "nobody", "run-as user after port binding")
	flag.StringVar(&a.BootstrapSecret, "secret", "", "bootstrap signing secret")

	flag.Parse()

	_, err := user.Lookup(a.User)
	if err != nil {
		return nil, err
	}

	region, az, err := parseRegionAZ(a.regionAZ)
	if err != nil {
		return nil, err
	}
	a.Region = region
	a.AvailabilityZone = az

	act, err := parseAccountFromARN(a.RoleARN)
	if err != nil {
		return nil, err
	}
	a.AccountNumber = act

	if a.BootstrapSecret == "" {
		return nil, errors.New("Bootstrap secret must be set")
	}

	return a, nil
}

func initialBootstrap(file string, handler CredentialHandler) {
	bd, err := ioutil.ReadFile(file)
	if err != nil {
		jww.ERROR.Printf("Error reading bootstrap file: %s", err.Error())
		return
	}

	var cred bootstrapInput
	err = json.Unmarshal(bd, &cred)
	if err != nil {
		jww.ERROR.Printf("Error decoding bootstrap JSON: %s", err)
		return
	}

	creds := credentials.NewStaticCredentials(cred.AccessKeyId, cred.SecretAccessKey, cred.Token)
	handler.SetBootstrapCredential(creds)
}

func main() {
	jww.SetStdoutThreshold(jww.LevelInfo)

	args, err := parseArgs()
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	// Bind this port first because it requires privilges then drop the
	// privileges before we do anything else
	metalistener, err := net.Listen("tcp", "169.254.169.254:80")
	if err != nil {
		jww.FATAL.Printf("Error setting up listener: %s", err.Error())
		return
	}

	if err := drop.DropPrivileges(args.User); err != nil {
		jww.FATAL.Printf("Unable to drop privileges")
		return
	}

	iface, err := getInterfaceContext()
	if err != nil {
		jww.FATAL.Printf("Error getting network interface info: %s", err)
		return
	}

	credHandler := NewCredentialHandler(
		&args.Region,
		&args.RoleARN,
		iface.PrimaryHostname,
	)

	ctx := &appContext{
		PrivateIP:         iface.PrimaryIP,
		Region:            &args.Region,
		MacAddr:           iface.MacAddr,
		AvailabilityZone:  &args.AvailabilityZone,
		Hostname:          iface.PrimaryHostname,
		InstanceId:        generateInstanceId(iface.PrimaryHostname),
		InstanceType:      &args.InstanceType,
		AccountId:         &args.AccountNumber,
		ReservationId:     generatePlausibleId("r"),
		ImageId:           generatePlausibleId("ami"),
		InstanceProfileId: generatePlausibleProfileId(),
		RoleARN:           &args.RoleARN,
		BootstrapSecret:   &args.BootstrapSecret,
		CredentialHandler: credHandler,
	}

	initialBootstrap("/etc/mmds/bootstrap-cred.json", credHandler)

	go credHandler.Start()
	go http.ListenAndServe(":8000", buildAdminHandler(ctx))
	ListenAndServeRaw(metalistener, buildMetadataHandler(ctx, DEFAULT_VALUES))
}