Cấu hình Role-Based Access Control (RBAC) trong Backstage và Kubernetes
Để bảo vệ hạ tầng, bạn cần định nghĩa rõ ràng ai được phép xem, sửa hoặc xóa tài nguyên trong Backstage và Kubernetes. Chúng ta sẽ dùng plugin permission-backend của Backstage để quản lý quyền truy cập dựa trên vai trò (Role) và nhóm (Group).
Bước 1: Cài đặt plugin quyền hạn vào Backend của Backstage để xử lý logic phân quyền.
cd /opt/backstage
npm install @backstage/plugin-permission-node @backstage/plugin-permission-common @backstage/plugin-permission-react
Kết quả mong đợi: Các gói thư viện được cài đặt vào thư mục node_modules và file package.json được cập nhật.
Bước 2: Cấu hình file app-config.yaml để định nghĩa các Role và Group. File này nằm ở /opt/backstage/app-config.yaml.
Thay thế hoặc bổ sung phần permission và permission.policy như sau:
permission:
enabled: true
policy:
default: allow
rules:
- permission: catalog-entity.create
subject:
kind: group
attributes:
name: platform-admins
effect: allow
- permission: catalog-entity.update
subject:
kind: group
attributes:
name: platform-admins
effect: allow
- permission: catalog-entity.delete
subject:
kind: group
attributes:
name: platform-admins
effect: allow
- permission: scaffolder-template.create
subject:
kind: user
attributes:
name: .*
effect: allow
Kết quả mong đợi: Backstage sẽ chặn các hành động tạo, sửa, xóa entity catalog nếu user không thuộc nhóm platform-admins.
Bước 3: Áp dụng RBAC tương ứng vào Kubernetes để bảo vệ namespace của Backstage.
Tạo file rbac-backstage.yaml tại /opt/backstage/config/k8s/rbac-backstage.yaml với nội dung:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: backstage-scaffolder-runner
namespace: backstage
rules:
- apiGroups: [""]
resources: ["secrets", "configmaps"]
verbs: ["get", "list", "watch", "create", "update", "delete"]
- apiGroups: ["apps"]
resources: ["deployments", "statefulsets"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: backstage-scaffolder-runner
namespace: backstage
subjects:
- kind: ServiceAccount
name: backstage-scaffolder
namespace: backstage
roleRef:
kind: Role
name: backstage-scaffolder-runner
apiGroup: rbac.authorization.k8s.io
Kết quả mong đợi: ServiceAccount backstage-scaffolder trong namespace backstage có quyền thao tác tài nguyên K8s cần thiết cho việc tự động hóa.
Cách verify: Login vào Backstage với user thường, cố gắng sửa một Entity Catalog. Hệ thống phải báo lỗi "Access Denied" hoặc không hiển thị nút sửa. Sau đó, login với user thuộc nhóm platform-admins, nút sửa phải xuất hiện.
Tùy chỉnh plugin Scaffolder để hỗ trợ nhiều loại template dự án
Plugin Scaffolder mặc định chỉ hỗ trợ một vài template cơ bản. Để hỗ trợ đa dạng stack (Java, Go, React, Python), bạn cần tạo thư mục templates riêng và cấu hình để Backstage đọc chúng.
Bước 1: Tạo thư mục chứa các template tùy chỉnh tại /opt/backstage/scaffolder-templates.
mkdir -p /opt/backstage/scaffolder-templates
Kết quả mong đợi: Thư mục mới được tạo, sẵn sàng chứa các template YAML.
Bước 2: Tạo template cho dự án Go (Go Boilerplate) tại /opt/backstage/scaffolder-templates/go-template/template.yaml.
apiVersion: scaffolder.backstage.io/v1beta3
kind: Template
metadata:
name: go-service-template
title: Go Service Boilerplate
description: Scaffold a new Go service with Docker and Kubernetes manifests.
tags:
- go
- backend
spec:
owner: platform-admins
type: service
parameters:
- title: Provide project details
required:
- name
- description
properties:
name:
title: Name
type: string
description: Unique name of the service
ui:autofocus: true
description:
title: Description
type: string
description: A short description of the service
- title: Configure Git Repository
required:
- repoUrl
properties:
repoUrl:
title: Repository URL
type: string
ui:field: RepoUrlPicker
description: Where should the new service be created?
steps:
- id: fetch-base
name: Fetch Base
action: fetch:template
params:
url: ./templates
values:
name: ${{ parameters.name }}
- id: select-remote
name: Select Repository
action: git:repo
params:
description: Select where to create the new service
repoUrl: ${{ parameters.repoUrl }}
defaultBranch: main
- id: push
name: Push
action: git:push
params:
description: Push to the new service repository
repoUrl: ${{ parameters.repoUrl }}
- id: register
name: Register
action: catalog:register
params:
repoContentsUrl: ${{ steps.push.output.repoContentsUrl }}
catalogInfoPath: /catalog-info.yaml
output:
links:
- title: Open in GitHub
url: ${{ steps.push.output.remoteUrl }}
- title: Open in Backstage
icon: home
baseUrl: /
target: _blank
query:
filter: name
value: ${{ parameters.name }}
Kết quả mong đợi: File YAML mô tả quy trình tạo dự án Go hợp lệ.
Bước 3: Cấu hình app-config.yaml để Backstage đọc các template từ thư mục mới.
Sửa phần scaffolder trong /opt/backstage/app-config.yaml:
scaffolder:
catalog:
location:
type: url
target: ./scaffolder-templates
rules:
- allow: ['Template']
Kết quả mong đợi: Backstage Backend sẽ scan thư mục scaffolder-templates và hiển thị các template trong giao diện.
Bước 4: Tạo nội dung template thực tế (mẫu file code) tại /opt/backstage/scaffolder-templates/go-template/templates/catalog-info.yaml.
apiVersion: backstage.io/v1alpha1
kind: Component
metadata:
name: ${{ values.name }}
description: ${{ values.description }}
spec:
type: service
lifecycle: experimental
owner: platform-admins
Kết quả mong đợi: Khi chạy template, file catalog-info.yaml sẽ được tạo với thông tin thực tế từ người dùng.
Cách verify: Vào trang "Create" trong Backstage. Tìm template "Go Service Boilerplate". Điền thông tin và click "Create". Sau đó vào GitHub/Repo để kiểm tra xem code và file catalog-info.yaml đã được tạo đúng chưa.
Triển khai plugin Home và Software Templates để hướng dẫn developer
Để developer mới dễ dàng bắt đầu, chúng ta cần trang Home thân thiện và khu vực "Software Templates" rõ ràng. Mặc dù plugin Home thường đã được cài sẵn, chúng ta cần cấu hình nó để hiển thị đúng các widget quan trọng.
Bước 1: Cài đặt plugin Home và plugin Software Templates vào Frontend của Backstage.
cd /opt/backstage
npm install @backstage/plugin-home @backstage/plugin-scaffolder @backstage/plugin-scaffolder-node
Kết quả mong đợi: Các gói frontend và backend node của scaffolder được cài đặt.
Bước 2: Cấu hình file app-config.yaml để kích hoạt và hiển thị plugin Home.
Sửa phần home trong /opt/backstage/app-config.yaml:
home:
enabled: true
showCheatSheet: true
showOnboarding: true
Kết quả mong đợi: Trang chủ Backstage sẽ hiển thị các widget hướng dẫn và cheat sheet.
Bước 3: Cấu hình plugin Software Templates trong app-config.yaml để đảm bảo nó đọc từ nguồn template đã tạo ở phần trước.
scaffolder:
catalog:
location:
type: url
target: ./scaffolder-templates
rules:
- allow: ['Template']
templates:
- title: Default Templates
description: Default scaffolding templates
location: ./scaffolder-templates
Kết quả mong đợi: Plugin Scaffolder sẽ liệt kê đầy đủ các template đã tạo.
Bước 4: Tạo một file README hướng dẫn trong thư mục template để developer đọc ngay khi tạo dự án.
Tạo file /opt/backstage/scaffolder-templates/go-template/templates/README.md:
# Go Service Boilerplate
This template helps you scaffold a new Go service.
## How to use
1. Select this template in Backstage.
2. Provide your project name and description.
3. Choose your Git repository location.
4. Backstage will create the code, register it in the catalog, and push to Git.
## Next Steps
- Run `go build` to compile.
- Run `docker build -t my-go-app .` to build the image.
- Deploy using `kubectl apply -f k8s/`.
Kết quả mong đợi: Khi developer vào xem template trong Backstage, họ sẽ thấy mô tả chi tiết này.
Cách verify: Mở trình duyệt, vào trang chủ (Home). Kiểm tra xem widget "Create" có hiện không. Click vào "Software Templates", đảm bảo template Go Service xuất hiện và hiển thị mô tả README.md.
Tích hợp SSO (OIDC) để quản lý người dùng và quyền truy cập
Để quản lý người dùng tập trung, Backstage cần tích hợp với Identity Provider (IdP) thông qua giao thức OIDC (OpenID Connect). Chúng ta sẽ cấu hình Backstage để sử dụng Google OIDC hoặc Keycloak làm ví dụ.
Bước 1: Cài đặt plugin Auth Backend và Auth Node.
cd /opt/backstage
npm install @backstage/plugin-auth-backend @backstage/plugin-auth-node @backstage/plugin-auth-react
Kết quả mong đợi: Các gói thư viện xác thực được cài đặt.
Bước 2: Cấu hình provider OIDC trong app-config.yaml tại /opt/backstage/app-config.yaml.
Thay thế phần auth như sau (đây là ví dụ với Keycloak, thay đổi issuer và clientId theo IdP của bạn):
auth:
environment: development
providers:
oidc:
development:
metadataUrl: https://keycloak.example.com/realms/my-realm/.well-known/openid-configuration
clientId: backstage
clientSecret: YOUR_CLIENT_SECRET_HERE
signInResolver:
type: legacy
legacy:
enabled: true
Kết quả mong đợi: Backstage biết cách liên kết với IdP để lấy token JWT.
Bước 3: Tạo file auth-backend-config.yaml riêng biệt để bảo mật các secret.
Tạo file /opt/backstage/config/auth-backend-config.yaml:
auth:
providers:
oidc:
development:
metadataUrl: https://keycloak.example.com/realms/my-realm/.well-known/openid-configuration
clientId: backstage
clientSecret: YOUR_CLIENT_SECRET_HERE
tokenIntrospectionUrl: https://keycloak.example.com/realms/my-realm/protocol/openid-connect/token/introspect
Kết quả mong đợi: Backend của Backstage có thể xác thực token từ IdP.
Bước 4: Cấu hình Kubernetes để inject biến môi trường chứa secret vào pod Backstage.
Sửa file deployment của Backstage (ví dụ backstage-backend-deployment.yaml) tại /opt/backstage/config/k8s/backstage-backend-deployment.yaml để thêm envFrom hoặc secretKeyRef:
apiVersion: apps/v1
kind: Deployment
metadata:
name: backstage-backend
namespace: backstage
spec:
template:
spec:
containers:
- name: backstage-backend
image: backstage-backend:latest
envFrom:
- secretRef:
name: backstage-auth-secrets
Và tạo secret tương ứng:
kubectl create secret generic backstage-auth-secrets \
--from-literal=BACKSTAGE_CONFIG__AUTH__PROVIDERS__OIDC__DEVELOPMENT__CLIENT_SECRET=YOUR_CLIENT_SECRET_HERE \
-n backstage
Kết quả mong đợi: Pod Backstage Backend khởi động với thông tin xác thực OIDC từ Kubernetes Secret.
Bước 5: Cấu hình plugin Auth Frontend trong app-config.yaml để frontend biết cách gọi API xác thực.
app:
title: My Internal Developer Platform
baseUrl: https://backstage.example.com
authentication:
externalIdentityProviders:
- id: oidc
providerId: oidc
type: oidc
displayName: Keycloak
icon: keycloak
loginApiEndpoint: /api/auth/oidc
Kết quả mong đợi: Giao diện Backstage hiện nút đăng nhập SSO.
Cách verify: Mở trang Backstage trong trình duyệt. Bạn sẽ thấy nút "Sign in with Keycloak" (hoặc IdP của bạn). Click vào, nhập thông tin IdP, sau đó đăng nhập thành công. Kiểm tra tên người dùng hiển thị trên giao diện và đảm bảo user này có quyền truy cập theo RBAC đã cấu hình ở phần đầu.
Điều hướng series:
Mục lục: Series: Series: Xây dựng nền tảng Internal Developer Platform (IDP) với Backstage, Crossplane và GitOps trên Kubernetes để tối ưu trải nghiệm phát triển phần mềm
« Phần 5: Tích hợp Backstage với Crossplane và GitOps
Phần 7: Tối ưu hóa hiệu năng và mở rộng quy mô »