Skip to content

Commit

Permalink
Added policies
Browse files Browse the repository at this point in the history
  • Loading branch information
nvnyale committed May 12, 2022
1 parent c48bb62 commit 11782be
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
16 changes: 12 additions & 4 deletions api/handlers_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func (s *server) InstanceSendCommandHandler(w http.ResponseWriter, r *http.Reque

}

func (s *server) InstanceIDHandler(w http.ResponseWriter, r *http.Request) {
func (s *server) NotImplementedHandler(w http.ResponseWriter, r *http.Request) {
w = LogWriter{w}
w.WriteHeader(http.StatusNotImplemented)
}
Expand All @@ -468,12 +468,17 @@ func (s *server) InstanceSSMAssociationHandler(w http.ResponseWriter, r *http.Re
}

role := fmt.Sprintf("arn:aws:iam::%s:role/%s", account, s.session.RoleName)
policy, err := ssmAssociationPolicy()
if err != nil {
handleError(w, err)
return
}

session, err := s.assumeRole(
r.Context(),
s.session.ExternalID,
role,
"",
policy,
"arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess",
)
if err != nil {
Expand Down Expand Up @@ -509,12 +514,15 @@ func (s *server) InstanceUpdateHandler(w http.ResponseWriter, r *http.Request) {
}

if len(req.Tags) == 0 && len(req.InstanceType) == 0 {
handleError(w, apierror.New(apierror.ErrBadRequest, "missing required fields", nil))
handleError(w, apierror.New(apierror.ErrBadRequest, "missing required fields: tags or instance_type", nil))
return
} else if len(req.Tags) > 0 && len(req.InstanceType) > 0 {
handleError(w, apierror.New(apierror.ErrBadRequest, "only one of these fields should be provided: tags or instance_type", nil))
return
}

role := fmt.Sprintf("arn:aws:iam::%s:role/%s", account, s.session.RoleName)
policy, err := tagCreatePolicy()
policy, err := instanceUpdatePolicy()
if err != nil {
handleError(w, err)
return
Expand Down
48 changes: 48 additions & 0 deletions api/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,51 @@ func sendCommandPolicy() (string, error) {

return string(j), nil
}

func instanceUpdatePolicy() (string, error) {
log.Debugf("generating tag create policy document")
policy := iam.PolicyDocument{
Version: "2012-10-17",
Statement: []iam.StatementEntry{
{
Effect: "Allow",
Action: []string{
"ec2:CreateTags",
"ec2:ModifyInstanceAttribute",
},
Resource: []string{"*"},
},
},
}

j, err := json.Marshal(policy)
if err != nil {
return "", err
}

return string(j), nil
}

func ssmAssociationPolicy() (string, error) {
log.Debugf("generating tag create policy document")
policy := iam.PolicyDocument{
Version: "2012-10-17",
Statement: []iam.StatementEntry{
{
Effect: "Allow",
Action: []string{
"ssm:CreateAssociation",
"ssm:UpdateAssociation",
},
Resource: []string{"*"},
},
},
}

j, err := json.Marshal(policy)
if err != nil {
return "", err
}

return string(j), nil
}
2 changes: 1 addition & 1 deletion api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *server) routes() {
api.HandleFunc("/{account}/images", s.ProxyRequestHandler).Methods(http.MethodPost)

api.HandleFunc("/{account}/images/{id}/tags", s.ImageUpdateHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}", s.InstanceIDHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}", s.NotImplementedHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/power", s.InstanceStateHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/ssm/command", s.InstanceSendCommandHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/ssm/association", s.InstanceSSMAssociationHandler).Methods(http.MethodPut)
Expand Down

0 comments on commit 11782be

Please sign in to comment.