Commit 77af9b06 authored by Tim Gross's avatar Tim Gross
Browse files

provide method to generate identity claim from allocation

parent d31a393b
Showing with 44 additions and 2 deletions
+44 -2
......@@ -191,7 +191,7 @@ require (
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/gojuno/minimock/v3 v3.0.6 // indirect
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
github.com/golang-jwt/jwt/v4 v4.4.1
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135 // indirect
......
......@@ -8,4 +8,5 @@ codecgen \
-d 100 \
-t codegen_generated \
-o structs.generated.go \
-nr="^IdentityClaims$" \
${FILES}
......@@ -24,6 +24,7 @@ import (
"strings"
"time"
jwt "github.com/golang-jwt/jwt/v4"
"github.com/hashicorp/nomad/helper/escapingfs"
"golang.org/x/crypto/blake2b"
......@@ -9579,6 +9580,11 @@ type Allocation struct {
// to stop running because it got preempted
PreemptedByAllocation string
// SignedIdentities is a map of task names to signed
// identity/capability claim tokens for those tasks. If needed, it
// is populated in the plan applier
SignedIdentities map[string]string `json:"-"`
// Raft Indexes
CreateIndex uint64
ModifyIndex uint64
......@@ -10273,6 +10279,40 @@ func (a *Allocation) Reconnected() (bool, bool) {
return true, a.Expired(lastReconnect)
}
func (a *Allocation) ToIdentityClaims() *IdentityClaims {
now := jwt.NewNumericDate(time.Now().UTC())
return &IdentityClaims{
Namespace: a.Namespace,
AllocationID: a.ID,
RegisteredClaims: jwt.RegisteredClaims{
// TODO: in Nomad 1.5.0 we'll have a refresh loop to
// prevent allocation identities from expiring before the
// allocation is terminal. Once that's implemented, add an
// ExpiresAt here ExpiresAt: &jwt.NumericDate{},
NotBefore: now,
IssuedAt: now,
},
}
}
func (a *Allocation) ToTaskIdentityClaims(taskName string) *IdentityClaims {
claims := a.ToIdentityClaims()
if claims != nil {
claims.TaskName = taskName
}
return claims
}
// IdentityClaims are the input to a JWT identifying a workload. It
// should never be serialized to msgpack unsigned.
type IdentityClaims struct {
Namespace string `json:"nomad_namespace"`
AllocationID string `json:"nomad_allocation_id"`
TaskName string `json:"nomad_task"`
jwt.RegisteredClaims
}
// AllocationDiff is another named type for Allocation (to use the same fields),
// which is used to represent the delta for an Allocation. If you need a method
// defined on the al
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment