forked from knative/eventing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
246 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
pkg/reconciler/pingsource/resources/oidc_rolebinding.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
Copyright 2020 The Knative Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
// taken from #7452, with modifications for pingsource | ||
|
||
package resources | ||
|
||
import ( | ||
"fmt" | ||
|
||
"knative.dev/eventing/pkg/apis/sources" | ||
|
||
"knative.dev/pkg/kmeta" | ||
|
||
rbacv1 "k8s.io/api/rbac/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
v1 "knative.dev/eventing/pkg/apis/sources/v1" | ||
) | ||
|
||
// GetOIDCTokenRoleName will return the name of the role for creating the JWT token | ||
func GetOIDCTokenRoleName(sourceName string) string { | ||
return kmeta.ChildName(sourceName, "-create-oidc-token") | ||
} | ||
|
||
// GetOIDCTokenRoleBindingName will return the name of the rolebinding for creating the JWT token | ||
func GetOIDCTokenRoleBindingName(sourceName string) string { | ||
return kmeta.ChildName(sourceName, "-create-oidc-token") | ||
} | ||
|
||
func MakeOIDCRole(source *v1.PingSource) (*rbacv1.Role, error) { | ||
roleName := GetOIDCTokenRoleName(source.Name) | ||
|
||
if source.Status.Auth == nil || source.Status.Auth.ServiceAccountName == nil { | ||
return nil, fmt.Errorf("Error when making OIDC Role for pingsource, as the OIDC service account does not exist") | ||
} | ||
|
||
return &rbacv1.Role{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: roleName, | ||
Namespace: source.GetNamespace(), | ||
Annotations: map[string]string{ | ||
"description": fmt.Sprintf("Role for OIDC Authentication for PingSource %q", source.GetName()), | ||
}, | ||
Labels: map[string]string{ | ||
sources.OIDCLabelKey: "", | ||
}, | ||
OwnerReferences: []metav1.OwnerReference{ | ||
*kmeta.NewControllerRef(source), | ||
}, | ||
}, | ||
Rules: []rbacv1.PolicyRule{ | ||
rbacv1.PolicyRule{ | ||
APIGroups: []string{""}, | ||
// apiServerSource OIDC service account name, it is in the source.Status, NOT in source.Spec | ||
ResourceNames: []string{*source.Status.Auth.ServiceAccountName}, | ||
Resources: []string{"serviceaccounts/token"}, | ||
Verbs: []string{"create"}, | ||
}, | ||
}, | ||
}, nil | ||
|
||
} | ||
|
||
// MakeOIDCRoleBinding will return the rolebinding object for generating the JWT token | ||
func MakeOIDCRoleBinding(source *v1.PingSource) (*rbacv1.RoleBinding, error) { | ||
roleName := GetOIDCTokenRoleName(source.Name) | ||
roleBindingName := GetOIDCTokenRoleBindingName(source.Name) | ||
|
||
if *source.Status.Auth.ServiceAccountName == "" { | ||
return nil, fmt.Errorf("Error when making OIDC RoleBinding for pingserversource, as the Spec service account does not exist") | ||
} | ||
|
||
return &rbacv1.RoleBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: roleBindingName, | ||
Namespace: source.GetNamespace(), | ||
Annotations: map[string]string{ | ||
"description": fmt.Sprintf("Role Binding for OIDC Authentication for PingServerSource %q", source.GetName()), | ||
}, | ||
Labels: map[string]string{ | ||
sources.OIDCLabelKey: "", | ||
}, | ||
OwnerReferences: []metav1.OwnerReference{ | ||
*kmeta.NewControllerRef(source), | ||
}, | ||
}, | ||
RoleRef: rbacv1.RoleRef{ | ||
APIGroup: "rbac.authorization.k8s.io", | ||
Kind: "Role", | ||
Name: roleName, | ||
}, | ||
Subjects: []rbacv1.Subject{ | ||
{ | ||
Kind: "ServiceAccount", | ||
Namespace: source.GetNamespace(), | ||
//Note: apiServerSource service account name, it is in the source.Spec, NOT in source.Status.Auth | ||
Name: *source.Status.Auth.ServiceAccountName, | ||
}, | ||
}, | ||
}, nil | ||
|
||
} |