Skip to content
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 commit sha and trigger to github workflow #232

Merged
merged 2 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pkg/ca/googlecabeta/googleca.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,28 @@ func githubWorkflowSubject(id string) *privatecapb.CertificateConfig_SubjectConf
}
}

func GithubWorkflowExtension(info challenges.WorkflowResult) []*privatecapb.X509Extension {
res := []*privatecapb.X509Extension{}
if info.Sha != "" {
res = append(res, &privatecapb.X509Extension{
ObjectId: &privatecapb.ObjectId{
ObjectIdPath: []int32{1, 3, 6, 1, 4, 1, 57264, 1, 2},
},
Value: []byte(info.Sha),
})
}

if info.Trigger != "" {
res = append(res, &privatecapb.X509Extension{
ObjectId: &privatecapb.ObjectId{
ObjectIdPath: []int32{1, 3, 6, 1, 4, 1, 57264, 1, 3},
},
Value: []byte(info.Trigger),
})
}
return res
}

func KubernetesSubject(id string) *privatecapb.CertificateConfig_SubjectConfig {
return &privatecapb.CertificateConfig_SubjectConfig{
SubjectAltName: &privatecapb.SubjectAltNames{
Expand Down Expand Up @@ -184,7 +206,7 @@ func (c *CertAuthorityService) CreateCertificate(ctx context.Context, subj *chal
return nil, ca.ValidationError(err)
}

extensions := IssuerExtension(subj.Issuer)
extensions := append(IssuerExtension(subj.Issuer), GithubWorkflowExtension(subj.WorkflowInfo)...)

req, err := Req(c.parent, privca, pubKeyBytes, extensions)
if err != nil {
Expand Down
20 changes: 19 additions & 1 deletion pkg/ca/x509ca/x509ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (x *X509CA) CreateCertificateWithCA(certauth *X509CA, subject *challenges.C
}
cert.URIs = []*url.URL{k8sURI}
}
cert.ExtraExtensions = IssuerExtension(subject.Issuer)
cert.ExtraExtensions = append(IssuerExtension(subject.Issuer), GithubWorkflowExtension(subject.WorkflowInfo)...)

finalCertBytes, err := x509.CreateCertificate(rand.Reader, cert, certauth.RootCA, subject.PublicKey, certauth.PrivKey)
if err != nil {
Expand All @@ -134,6 +134,24 @@ func (x *X509CA) CreateCertificateWithCA(certauth *X509CA, subject *challenges.C
return ca.CreateCSCFromDER(subject, finalCertBytes, nil)
}

func GithubWorkflowExtension(info challenges.WorkflowResult) []pkix.Extension {
res := []pkix.Extension{}
if info.Sha != "" {
res = append(res, pkix.Extension{
Id: asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 57264, 1, 2},
Value: []byte(info.Sha),
})
}

if info.Trigger != "" {
res = append(res, pkix.Extension{
Id: asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 57264, 1, 3},
Value: []byte(info.Trigger),
})
}
return res
}

func IssuerExtension(issuer string) []pkix.Extension {
if issuer == "" {
return nil
Expand Down
42 changes: 37 additions & 5 deletions pkg/challenges/challenges.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ const (
KubernetesValue
)

type WorkflowResult struct {
// Additional information associated with a Github Workflow
Trigger string
Sha string
}

type ChallengeResult struct {
Issuer string
TypeVal ChallengeType
PublicKey crypto.PublicKey
Value string
// Extra information, non-empty for a Github workflow
WorkflowInfo WorkflowResult
asraa marked this conversation as resolved.
Show resolved Hide resolved
}

func CheckSignature(pub crypto.PublicKey, proof []byte, email string) error {
Expand Down Expand Up @@ -165,6 +173,11 @@ func GithubWorkflow(ctx context.Context, principal *oidc.IDToken, pubKey crypto.
if err != nil {
return nil, err
}
// Additional info
workflowInfo, err := workflowInfoFromIDToken(principal)
if err != nil {
return nil, err
}

// Check the proof
if err := CheckSignature(pubKey, challenge, principal.Subject); err != nil {
Expand All @@ -184,10 +197,11 @@ func GithubWorkflow(ctx context.Context, principal *oidc.IDToken, pubKey crypto.

// Now issue cert!
return &ChallengeResult{
Issuer: issuer,
PublicKey: pubKey,
TypeVal: GithubWorkflowValue,
Value: workflowRef,
Issuer: issuer,
PublicKey: pubKey,
TypeVal: GithubWorkflowValue,
Value: workflowRef,
WorkflowInfo: workflowInfo,
}, nil
}

Expand Down Expand Up @@ -240,6 +254,25 @@ func workflowFromIDToken(token *oidc.IDToken) (string, error) {
return "https://github.com/" + claims.JobWorkflowRef, nil
}

func workflowInfoFromIDToken(token *oidc.IDToken) (WorkflowResult, error) {
// Extract custom claims
var claims struct {
Sha string `json:"sha"`
Trigger string `json:"event_name"`
// The other fields that are present here seem to depend on the type
// of workflow trigger that initiated the action.
}
if err := token.Claims(&claims); err != nil {
return WorkflowResult{}, err
}

// We use this in URIs, so it has to be a URI.
return WorkflowResult{
Trigger: claims.Trigger,
Sha: claims.Sha,
}, nil
}

func isSpiffeIDAllowed(host, spiffeID string) bool {
// Strip spiffe://
name := strings.TrimPrefix(spiffeID, "spiffe://")
Expand All @@ -251,5 +284,4 @@ func isSpiffeIDAllowed(host, spiffeID string) bool {
return true
}
return strings.Contains(spiffeDomain, "."+host)

}