Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
552b307
feat: add sub-module to enable kube-audit in base-ocp clusters
Aashiq-J Mar 19, 2025
9d44de6
test
Aashiq-J Mar 19, 2025
3b16802
test
Aashiq-J Mar 19, 2025
9292d5d
test
Aashiq-J Mar 19, 2025
ccef77e
test
Aashiq-J Mar 19, 2025
684e97d
update advanced example
Aashiq-J Mar 20, 2025
e90cad9
documentation
Aashiq-J Mar 20, 2025
79ffe77
change image variable
Aashiq-J Mar 20, 2025
1bb31a8
add provider
Aashiq-J Mar 20, 2025
e061d4b
update sleep
Aashiq-J Mar 20, 2025
12f383c
update script
Aashiq-J Mar 20, 2025
ec3a5e6
Merge branch 'main' into audit
Aashiq-J Mar 21, 2025
ccbed82
review changes
Aashiq-J Mar 21, 2025
946278b
Merge branch 'main' into audit
Aashiq-J Mar 24, 2025
5f4b46a
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Mar 24, 2025
fba88e4
updates
Aashiq-J Mar 24, 2025
744e85d
test
Aashiq-J Mar 24, 2025
2e73fdc
test
Aashiq-J Mar 25, 2025
842d8f1
Merge branch 'main' into audit
Aashiq-J Mar 25, 2025
8259907
test
Aashiq-J Mar 28, 2025
3ced82b
test
Aashiq-J Mar 28, 2025
464c143
test
Aashiq-J Mar 28, 2025
67db02d
update
Aashiq-J Mar 28, 2025
afbc0ad
Merge branch 'main' into audit
Aashiq-J Mar 31, 2025
d7c852f
update
Aashiq-J Mar 31, 2025
b91a834
test
Aashiq-J Mar 31, 2025
3206a0a
test
Aashiq-J Apr 1, 2025
06a5a2a
Merge branch 'main' into audit
Aashiq-J Apr 1, 2025
4d7d049
test
Aashiq-J Apr 1, 2025
543c676
test
Aashiq-J Apr 1, 2025
df9c994
test
Aashiq-J Apr 1, 2025
3ea0e07
test
Aashiq-J Apr 1, 2025
980cfc5
test
Aashiq-J Apr 1, 2025
e67d2ce
test
Aashiq-J Apr 1, 2025
588c1ff
test
Aashiq-J Apr 1, 2025
f92906f
test
Aashiq-J Apr 1, 2025
ae8ba0c
test
Aashiq-J Apr 1, 2025
27c4aed
test
Aashiq-J Apr 2, 2025
94187cc
test
Aashiq-J Apr 2, 2025
0823497
test
Aashiq-J Apr 2, 2025
7966557
test
Aashiq-J Apr 2, 2025
8d041e0
test
Aashiq-J Apr 3, 2025
f190b17
test
Aashiq-J Apr 3, 2025
ba450c1
test
Aashiq-J Apr 3, 2025
4345fce
test
Aashiq-J Apr 3, 2025
5d7180d
Merge branch 'main' into audit
Aashiq-J Apr 3, 2025
ba93b82
Merge branch 'main' into audit
Aashiq-J Apr 3, 2025
7d3cce9
update
Aashiq-J Apr 4, 2025
c2a6de6
Merge branch 'main' into audit
imprateeksh Apr 5, 2025
72bdce3
Merge branch 'main' into audit
Aashiq-J Apr 8, 2025
bc76879
Merge branch 'main' into audit
Aashiq-J Apr 14, 2025
7594159
Merge branch 'main' of https://github.com/terraform-ibm-modules/terra…
Aashiq-J Apr 15, 2025
3969896
update
Aashiq-J Apr 15, 2025
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Optionally, the module supports advanced security group management for the worke
* [terraform-ibm-base-ocp-vpc](#terraform-ibm-base-ocp-vpc)
* [Submodules](./modules)
* [fscloud](./modules/fscloud)
* [kube-audit](./modules/kube-audit)
* [Examples](./examples)
* [2 MZR clusters in same VPC example](./examples/multiple_mzr_clusters)
* [Advanced example (mzr, auto-scale, kms, taints)](./examples/advanced)
Expand Down
80 changes: 80 additions & 0 deletions examples/advanced/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,83 @@ data "ibm_container_cluster_config" "cluster_config" {
resource_group_id = module.ocp_base.resource_group_id
config_dir = "${path.module}/../../kubeconfig"
}

########################################################################################################################
# Kube Audit
########################################################################################################################

module "kube_audit" {
source = "../../modules/kube-audit"
cluster_id = module.ocp_base.cluster_id
cluster_resource_group_id = module.resource_group.resource_group_id
audit_log_policy = "WriteRequestBodies"
region = var.region
}


########################################################################################################################
# Observability (Instance + Agents)
########################################################################################################################

locals {
logs_agent_namespace = "ibm-observe"
logs_agent_name = "logs-agent"
}

module "observability_instances" {
source = "terraform-ibm-modules/observability-instances/ibm"
version = "3.4.3"
resource_group_id = module.resource_group.resource_group_id
region = var.region
cloud_logs_plan = "standard"
cloud_monitoring_plan = "graduated-tier"
enable_platform_metrics = false
cloud_logs_instance_name = "${var.prefix}-cloud-logs"
cloud_monitoring_provision = false
}

module "trusted_profile" {
source = "terraform-ibm-modules/trusted-profile/ibm"
version = "2.0.1"
trusted_profile_name = "${var.prefix}-profile"
trusted_profile_description = "Logs agent Trusted Profile"
# As a `Sender`, you can send logs to your IBM Cloud Logs service instance - but not query or tail logs. This role is meant to be used by agents and routers sending logs.
trusted_profile_policies = [{
roles = ["Sender"]
resources = [{
service = "logs"
}]
}]
# Set up fine-grained authorization for `logs-agent` running in ROKS cluster in `ibm-observe` namespace.
trusted_profile_links = [{
cr_type = "ROKS_SA"
links = [{
crn = module.ocp_base.cluster_crn
namespace = local.logs_agent_namespace
name = local.logs_agent_name
}]
}
]
}

module "observability_agents" {
depends_on = [module.kube_audit]
source = "terraform-ibm-modules/observability-agents/ibm"
version = "2.6.0"
cluster_id = module.ocp_base.cluster_id
cluster_resource_group_id = module.resource_group.resource_group_id
# Cloud Logs agent
logs_agent_trusted_profile = module.trusted_profile.trusted_profile.id
logs_agent_namespace = local.logs_agent_namespace
logs_agent_name = local.logs_agent_name
cloud_logs_ingress_endpoint = module.observability_instances.cloud_logs_ingress_private_endpoint
cloud_logs_ingress_port = 3443
# example of how to add additional metadata to the logs agents
logs_agent_additional_metadata = [{
key = "cluster_id"
value = module.ocp_base.cluster_id
}]
# example of how to add only kube-audit log source path
logs_agent_selected_log_source_paths = ["/var/log/audit/*.log"]
cloud_monitoring_enabled = false
}
14 changes: 14 additions & 0 deletions examples/advanced/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,17 @@ provider "kubernetes" {
token = data.ibm_container_cluster_config.cluster_config.token
cluster_ca_certificate = data.ibm_container_cluster_config.cluster_config.ca_certificate
}

provider "helm" {
kubernetes {
host = data.ibm_container_cluster_config.cluster_config.host
token = data.ibm_container_cluster_config.cluster_config.token
cluster_ca_certificate = data.ibm_container_cluster_config.cluster_config.ca_certificate
}
# IBM Cloud credentials are required to authenticate to the helm repo
registry {
url = "oci://icr.io/ibm/observe/logs-agent-helm"
username = "iamapikey"
password = var.ibmcloud_api_key
}
}
4 changes: 4 additions & 0 deletions examples/advanced/version.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ terraform {
source = "hashicorp/kubernetes"
version = ">= 2.16.1"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.15.0"
}
}
}
99 changes: 99 additions & 0 deletions modules/kube-audit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Kubernetes API server audit logs

To monitor user-initiated, Kubernetes administrative activity made within your cluster, you can collect and forward audit events that are passed through your Kubernetes API server to IBM Cloud Logs or an external server.

This sub-module helps you to create a Kubernetes audit system by using the provided image and deployment in your existing cluster. [Learn more](https://cloud.ibm.com/docs/openshift?topic=openshift-health-audit)

**Important**: The sub-module uses the `icr.io/ibm/ibmcloud-kube-audit-to-ibm-cloud-logs` image to forward logs to IBM Cloud Logs. This image is for demonstration purposes only. For a production solution, configure and maintain your own log forwarding image.

### Usage

```hcl
# ############################################################################
# Init cluster config for helm
# ############################################################################

data "ibm_container_cluster_config" "cluster_config" {
# update this value with the Id of the cluster where these agents will be provisioned
cluster_name_id = "cluster_id"
}

# ############################################################################
# Config providers
# ############################################################################

provider "ibm" {
# update this value with your IBM Cloud API key value
ibmcloud_api_key = "XXXXXXXXXXXXXXXXX" #pragma: allowlist secret
}

provider "helm" {
kubernetes {
host = data.ibm_container_cluster_config.cluster_config.host
token = data.ibm_container_cluster_config.cluster_config.token
cluster_ca_certificate = data.ibm_container_cluster_config.cluster_config.ca_certificate
}
}

provider "kubernetes" {
host = data.ibm_container_cluster_config.cluster_config.host
token = data.ibm_container_cluster_config.cluster_config.token
cluster_ca_certificate = data.ibm_container_cluster_config.cluster_config.ca_certificate
}

module "kube_audit" {
source = "terraform-ibm-modules/terraform-ibm-base-ocp-vpc/ibm//modules/kube-audit"
version = "X.X.X" # Replace "X.X.X" with a release version to lock into a specific release
cluster_id = "cluster_id"
cluster_resource_group_id = "resource group id"
region = "us-south"
}
```

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
### Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >=1.9.0 |
| <a name="requirement_helm"></a> [helm](#requirement\_helm) | >= 2.15.0, <3.0.0 |
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.70.0, <2.0.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1, < 4.0.0 |

### Modules

No modules.

### Resources

| Name | Type |
|------|------|
| [helm_release.kube_audit](https://registry.terraform.io/providers/hashicorp/helm/latest/docs/resources/release) | resource |
| [null_resource.set_audit_log_policy](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [null_resource.set_audit_webhook](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [ibm_container_cluster_config.cluster_config](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_cluster_config) | data source |
| [ibm_container_vpc_cluster.cluster](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/container_vpc_cluster) | data source |
| [ibm_iam_account_settings.iam_account_settings](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/iam_account_settings) | data source |
| [ibm_iam_auth_token.reset_api_key_tokendata](https://registry.terraform.io/providers/ibm-cloud/ibm/latest/docs/data-sources/iam_auth_token) | data source |

### Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_audit_deployment_name"></a> [audit\_deployment\_name](#input\_audit\_deployment\_name) | The name of log collection deployement and service. | `string` | `"ibmcloud-kube-audit"` | no |
| <a name="input_audit_log_policy"></a> [audit\_log\_policy](#input\_audit\_log\_policy) | Specify the amount of information that is logged to the API server audit logs by choosing the audit log policy profile to use. Supported values are `default` and `WriteRequestBodies`. | `string` | `"default"` | no |
| <a name="input_audit_namespace"></a> [audit\_namespace](#input\_audit\_namespace) | The name of the namespace where log collection service and a deployment will be created. | `string` | `"ibm-kube-audit"` | no |
| <a name="input_audit_webhook_listener_image"></a> [audit\_webhook\_listener\_image](#input\_audit\_webhook\_listener\_image) | The audit webhook listener image reference in the format of `[registry-url]/[namespace]/[image]`.The sub-module uses the `icr.io/ibm/ibmcloud-kube-audit-to-ibm-cloud-logs` image to forward logs to IBM Cloud Logs. This image is for demonstration purposes only. For a production solution, configure and maintain your own log forwarding image. | `string` | `"icr.io/ibm/ibmcloud-kube-audit-to-ibm-cloud-logs"` | no |
| <a name="input_audit_webhook_listener_image_digest"></a> [audit\_webhook\_listener\_image\_digest](#input\_audit\_webhook\_listener\_image\_digest) | An image digest in the format `sha256:xxxxx...` can also be specified. If no value is passed, latest image is used. | `string` | `null` | no |
| <a name="input_cluster_config_endpoint_type"></a> [cluster\_config\_endpoint\_type](#input\_cluster\_config\_endpoint\_type) | Specify which type of endpoint to use for for cluster config access: 'default', 'private', 'vpe', 'link'. 'default' value will use the default endpoint of the cluster. | `string` | `"default"` | no |
| <a name="input_cluster_id"></a> [cluster\_id](#input\_cluster\_id) | The ID of the cluster to deploy the log collection service in. | `string` | n/a | yes |
| <a name="input_cluster_resource_group_id"></a> [cluster\_resource\_group\_id](#input\_cluster\_resource\_group\_id) | The resource group ID of the cluster. | `string` | n/a | yes |
| <a name="input_region"></a> [region](#input\_region) | The IBM Cloud region where the cluster is provisioned. | `string` | n/a | yes |
| <a name="input_use_private_endpoint"></a> [use\_private\_endpoint](#input\_use\_private\_endpoint) | Set this to true to force all api calls to use the IBM Cloud private endpoints. | `bool` | `false` | no |
| <a name="input_wait_till"></a> [wait\_till](#input\_wait\_till) | To avoid long wait times when you run your Terraform code, you can specify the stage when you want Terraform to mark the cluster resource creation as completed. Depending on what stage you choose, the cluster creation might not be fully completed and continues to run in the background. However, your Terraform code can continue to run without waiting for the cluster to be fully created. Supported args are `MasterNodeReady`, `OneWorkerNodeReady`, `IngressReady` and `Normal` | `string` | `"Normal"` | no |
| <a name="input_wait_till_timeout"></a> [wait\_till\_timeout](#input\_wait\_till\_timeout) | Timeout for wait\_till in minutes. | `number` | `90` | no |

### Outputs

No outputs.
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
23 changes: 23 additions & 0 deletions modules/kube-audit/helm-charts/kube-audit/.helmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Patterns to ignore when building packages.
# This supports shell glob matching, relative path matching, and
# negation (prefixed with !). Only one pattern per line.
.DS_Store
# Common VCS dirs
.git/
.gitignore
.bzr/
.bzrignore
.hg/
.hgignore
.svn/
# Common backup files
*.swp
*.bak
*.tmp
*.orig
*~
# Various IDEs
.project
.idea/
*.tmproj
.vscode/
24 changes: 24 additions & 0 deletions modules/kube-audit/helm-charts/kube-audit/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v2
name: kube-audit
description: A Helm chart for kube-audit

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.2

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: "{{ .Values.metadata.name }}"
namespace: "{{ .Values.metadata.namespace }}"
labels:
app: "{{ .Values.metadata.name }}"
spec:
replicas: 1
selector:
matchLabels:
app: "{{ .Values.metadata.name }}"
template:
metadata:
labels:
app: "{{ .Values.metadata.name }}"
spec:
containers:
- name: "{{ .Values.metadata.name }}"
image: "{{ .Values.image }}"
imagePullPolicy: Always
ports:
- containerPort: 3000
securityContext:
allowPrivilegeEscalation: false
runAsNonRoot: true
capabilities:
drop:
- ALL
seccompProfile:
type: RuntimeDefault
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: v1
kind: Namespace
metadata:
name: "{{ .Values.metadata.namespace }}"
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/enforce-version: latest
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/audit-version: latest
pod-security.kubernetes.io/warn: restricted
pod-security.kubernetes.io/warn-version: latest
security.openshift.io/scc.podSecurityLabelSync: "false"
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: "{{ .Values.metadata.name }}"
namespace: "{{ .Values.metadata.namespace }}"
spec:
podSelector:
matchLabels:
app: "{{ .Values.metadata.name }}"
policyTypes:
- Ingress
ingress:
- ports:
- protocol: TCP
port: 3000
from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
app: konnectivity-agent
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: kube-system
podSelector:
matchLabels:
app: vpn
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: "{{ .Values.metadata.name }}-service"
namespace: "{{ .Values.metadata.namespace }}"
labels:
app: "{{ .Values.metadata.name }}"
spec:
selector:
app: "{{ .Values.metadata.name }}"
ports:
- protocol: TCP
port: 80
targetPort: 3000
type: ClusterIP
6 changes: 6 additions & 0 deletions modules/kube-audit/helm-charts/kube-audit/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# NOTE: Mock values added here for helm linter to pass. Actual values are set in main.tf
metadata:
name: "ibmcloud-kube-audit"
namespace: "ibm-kube-audit"

image: "icr.io/ibm/ibmcloud-kube-audit-to-ibm-cloud-logs"
Loading