Skip to content

Commit

Permalink
Tls client auth (#2584)
Browse files Browse the repository at this point in the history
* Add role and rolebindings for fetching client certificate for kube-apiserver

* Ops

* Changelog & fix role name
  • Loading branch information
DmitryDodzin authored Jul 10, 2024
1 parent b0f04a1 commit 9220123
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/+operator-client-ca-role.internal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add mirrord-operator-apiserver-authentication `Role` and `RoleBinding` to fetch `extension-apiserver-authentication` configmap from "kube-system".
80 changes: 78 additions & 2 deletions mirrord/operator/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use k8s_openapi::{
Probe, ResourceRequirements, Secret, SecretVolumeSource, SecurityContext, Service,
ServiceAccount, ServicePort, ServiceSpec, Volume, VolumeMount,
},
rbac::v1::{ClusterRole, ClusterRoleBinding, PolicyRule, RoleRef, Subject},
rbac::v1::{
ClusterRole, ClusterRoleBinding, PolicyRule, Role, RoleBinding, RoleRef, Subject,
},
},
apiextensions_apiserver::pkg::apis::apiextensions::v1::CustomResourceDefinition,
apimachinery::pkg::{
Expand All @@ -29,6 +31,7 @@ static OPERATOR_NAME: &str = "mirrord-operator";
static OPERATOR_PORT: i32 = 3000;
static OPERATOR_ROLE_NAME: &str = "mirrord-operator";
static OPERATOR_ROLE_BINDING_NAME: &str = "mirrord-operator";
static OPERATOR_CLIENT_CA_ROLE_NAME: &str = "mirrord-operator-apiserver-authentication";
static OPERATOR_CLUSTER_USER_ROLE_NAME: &str = "mirrord-operator-user";
static OPERATOR_LICENSE_SECRET_NAME: &str = "mirrord-operator-license";
static OPERATOR_LICENSE_SECRET_FILE_NAME: &str = "license.pem";
Expand Down Expand Up @@ -95,6 +98,8 @@ pub struct Operator {
service: OperatorService,
service_account: OperatorServiceAccount,
user_cluster_role: OperatorClusterUserRole,
client_ca_role: OperatorClientCaRole,
client_ca_role_binding: OperatorClientCaRoleBinding,
}

impl Operator {
Expand All @@ -118,6 +123,10 @@ impl Operator {
let role_binding = OperatorRoleBinding::new(&role, &service_account);
let user_cluster_role = OperatorClusterUserRole::new();

let client_ca_role = OperatorClientCaRole::new();
let client_ca_role_binding =
OperatorClientCaRoleBinding::new(&client_ca_role, &service_account);

let deployment = OperatorDeployment::new(
&namespace,
&service_account,
Expand All @@ -140,6 +149,8 @@ impl Operator {
service,
service_account,
user_cluster_role,
client_ca_role,
client_ca_role_binding,
}
}
}
Expand All @@ -162,9 +173,15 @@ impl OperatorSetup for Operator {
writer.write_all(b"---\n")?;
self.user_cluster_role.to_writer(&mut writer)?;

writer.write_all(b"---\n")?;
self.client_ca_role.to_writer(&mut writer)?;

writer.write_all(b"---\n")?;
self.role_binding.to_writer(&mut writer)?;

writer.write_all(b"---\n")?;
self.client_ca_role_binding.to_writer(&mut writer)?;

writer.write_all(b"---\n")?;
self.deployment.to_writer(&mut writer)?;

Expand Down Expand Up @@ -653,6 +670,63 @@ impl Default for OperatorClusterUserRole {
}
}

#[derive(Debug)]
pub struct OperatorClientCaRole(Role);

impl OperatorClientCaRole {
pub fn new() -> Self {
let role = Role {
metadata: ObjectMeta {
name: Some(OPERATOR_CLIENT_CA_ROLE_NAME.to_owned()),
namespace: Some("kube-system".to_owned()),
..Default::default()
},
rules: Some(vec![PolicyRule {
api_groups: Some(vec!["".to_owned()]),
resources: Some(vec!["configmaps".to_owned()]),
verbs: vec!["get".to_owned()],
resource_names: Some(vec!["extension-apiserver-authentication".to_owned()]),
..Default::default()
}]),
};

OperatorClientCaRole(role)
}

fn as_role_ref(&self) -> RoleRef {
RoleRef {
api_group: "rbac.authorization.k8s.io".to_owned(),
kind: "Role".to_owned(),
name: self.0.metadata.name.clone().unwrap_or_default(),
}
}
}

impl Default for OperatorClientCaRole {
fn default() -> Self {
Self::new()
}
}

#[derive(Debug)]
pub struct OperatorClientCaRoleBinding(RoleBinding);

impl OperatorClientCaRoleBinding {
pub fn new(role: &OperatorClientCaRole, sa: &OperatorServiceAccount) -> Self {
let role = RoleBinding {
metadata: ObjectMeta {
name: Some(OPERATOR_CLIENT_CA_ROLE_NAME.to_owned()),
namespace: role.0.metadata.namespace.clone(),
..Default::default()
},
role_ref: role.as_role_ref(),
subjects: Some(vec![sa.as_subject()]),
};

OperatorClientCaRoleBinding(role)
}
}

impl OperatorSetup for CustomResourceDefinition {
fn to_writer<W: Write>(&self, writer: W) -> Result<()> {
serde_yaml::to_writer(writer, &self).map_err(SetupWriteError::from)
Expand All @@ -668,5 +742,7 @@ writer_impl![
OperatorLicenseSecret,
OperatorService,
OperatorApiService,
OperatorClusterUserRole
OperatorClusterUserRole,
OperatorClientCaRole,
OperatorClientCaRoleBinding
];

0 comments on commit 9220123

Please sign in to comment.