-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add token create forms to Join Tokens UI #44408
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,7 +147,12 @@ type CreateTokenRequest struct { | |
Content string `json:"content"` | ||
} | ||
|
||
func (h *Handler) upsertTokenContent(w http.ResponseWriter, r *http.Request, params httprouter.Params, sctx *SessionContext) (interface{}, error) { | ||
func (h *Handler) updateTokenYAML(w http.ResponseWriter, r *http.Request, params httprouter.Params, sctx *SessionContext) (interface{}, error) { | ||
tokenId := r.Header.Get(HeaderTokenName) | ||
if tokenId == "" { | ||
return nil, trace.BadParameter("requires a token name to edit") | ||
} | ||
|
||
var yaml CreateTokenRequest | ||
if err := httplib.ReadJSON(r, &yaml); err != nil { | ||
return nil, trace.Wrap(err) | ||
|
@@ -158,6 +163,10 @@ func (h *Handler) upsertTokenContent(w http.ResponseWriter, r *http.Request, par | |
return nil, trace.Wrap(err) | ||
} | ||
|
||
if tokenId != extractedRes.Metadata.Name { | ||
return nil, trace.BadParameter("renaming tokens is not supported") | ||
} | ||
|
||
token, err := services.UnmarshalProvisionToken(extractedRes.Raw) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
|
@@ -182,7 +191,69 @@ func (h *Handler) upsertTokenContent(w http.ResponseWriter, r *http.Request, par | |
|
||
} | ||
|
||
func (h *Handler) createTokenHandle(w http.ResponseWriter, r *http.Request, params httprouter.Params, ctx *SessionContext) (interface{}, error) { | ||
type upsertTokenHandleRequest struct { | ||
types.ProvisionTokenSpecV2 | ||
Name string `json:"name"` | ||
} | ||
|
||
func (h *Handler) upsertTokenHandle(w http.ResponseWriter, r *http.Request, params httprouter.Params, ctx *SessionContext) (interface{}, error) { | ||
// if using the PUT route, tokenId will be present | ||
// in the X-Teleport-TokenName header | ||
editing := r.Method == "PUT" | ||
tokenId := r.Header.Get(HeaderTokenName) | ||
if editing && tokenId == "" { | ||
return nil, trace.BadParameter("requires a token name to edit") | ||
} | ||
|
||
var req upsertTokenHandleRequest | ||
if err := httplib.ReadJSON(r, &req); err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
if editing && tokenId != req.Name { | ||
return nil, trace.BadParameter("renaming tokens is not supported") | ||
} | ||
|
||
// set expires time to default node join token TTL | ||
expires := time.Now().UTC().Add(defaults.NodeJoinTokenTTL) | ||
// IAM and GCP tokens should never expire | ||
if req.JoinMethod == types.JoinMethodGCP || req.JoinMethod == types.JoinMethodIAM { | ||
expires = time.Now().UTC().AddDate(1000, 0, 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a const for 1000? to consistently set a "never expires" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could add a constant but the time package doesn't have |
||
} | ||
|
||
name := req.Name | ||
if name == "" { | ||
randName, err := utils.CryptoRandomHex(defaults.TokenLenBytes) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
name = randName | ||
} | ||
|
||
token, err := types.NewProvisionTokenFromSpec(name, expires, req.ProvisionTokenSpecV2) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
clt, err := ctx.GetClient() | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
err = clt.UpsertToken(r.Context(), token) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
uiToken, err := ui.MakeJoinToken(token) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
|
||
return uiToken, nil | ||
} | ||
|
||
func (h *Handler) createTokenForDiscoveryHandle(w http.ResponseWriter, r *http.Request, params httprouter.Params, ctx *SessionContext) (interface{}, error) { | ||
clt, err := ctx.GetClient() | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this make the endpoint not backwards compatible? Should this use a new endpoint instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are new endpoints (I'm editing this one but it isn't actually used/released yet, just from my last PR) so no worry about backward compatibility here.