之前我们有介绍过如何使用 Aspire Dashboard 使用 aspire-dashboard 展示 open-telemetry trace/logging/metrics, 最近在将几个系统集成 Aspire 想要将 aspire dashboard 部署为自己系统的一部分,同时接入自己的用户系统来实现认证授权,发现 Aspire Dashboard 已经支持了 OpenIdConnect
的认证方式,集成起来还是比较简单的,下面介绍下 aspire dashboard 的各种认证方式
Aspire dashboard 的认证有三种方式
默认的是 Browser Token,可以通过一个 token 来访问,token 可以从 aspire dashboard 的 log 中获取
log
填入 log 中的 token 之后就可以访问了
如果本地为了方便不想登录,可以设置一个环境变量 DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS=true
这样就可以直接访问了,docker-compose 示例如下:
services:
aspire-dashboard:
container_name: aspire-dashboard
image: mcr.microsoft.com/dotnet/aspire-dashboard:9.0
ports:
- 18888:18888
- 4317:18889
environment:
DOTNET_DASHBOARD_UNSECURED_ALLOW_ANONYMOUS: "true"
第三种方式我们可以与已有的支持 OpenIdConnect 的系统做集成,可以通过自己的用户系统进行登录认证,docker-compose 示例如下:
services:
aspire-dashboard:
container_name:aspire-dashboard
image:mcr.microsoft.com/dotnet/aspire-dashboard:9.0
ports:
-18888:18888
-4317:18889
environment:
# OpenId Connect
Authentication__Schemes__OpenIdConnect__Authority:"https://id.weihanli.xyz"
Authentication__Schemes__OpenIdConnect__ClientId:"<client_id>"
Authentication__Schemes__OpenIdConnect__ClientSecret:"<client_secret>"
Authentication__Schemes__OpenIdConnect__GetClaimsFromUserInfoEndpoint:"true"
Dashboard__Frontend__AuthMode:"OpenIdConnect"
在自己的认证系统里创建一个 client,使用 authorization-code 方式进行认证,callback 地址为 aspire 的根路径地址如:http://localhost:18888/
OpenIdConnect Login
也可以进行更多的自定义,我们可以设置 OpenIdConnectOptions 里的更多配置比如,自定义 scope,可以加入想要使用的 scope 如 email 等,然后可以通过 RequiredClaimType
/RequiredClaimValue
来控制授权,如果配置了但是登录后的用户没有对应的 claim 就无法访问
{
"Dashboard": {
"Frontend": {
"AuthMode": "OpenIdConnect",
"OpenIdConnect": {
"RequiredClaimType": "email",
"RequiredClaimValue": "admin@weihanli.xyz"
}
}
}
}
这样我们就能更细致地控制访问权限了,在部署的时候发现如果要使用角色(role
)来控制会有点问题,在登录成功之后没有 role
相关的 claim type,可以参考 issue:https://github.com/dotnet/aspire/issues/7755,如果你也有这个问题可以先使用 email/sub 之类的 claim type 和 claim value 来验证,实现更细力度的权限控制
如果要部署在 nginx 之类的后面,需要配置请求转发头,可以给 aspire 配置 ASPNETCORE_FORWARDEDHEADERS_ENABLED=true
的环境变量以启用,同时要修改 nginx 的配置,登录 redirect 的时候才能拿到正确的地址,示例如下
server {
listen 443;
server_name aspire.weihanli.xyz;
location / {
proxy_pass http://172.18.0.2:31888;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Referer $http_referer;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Kubernetes 部署示例:
apiVersion: apps/v1
kind:Deployment
metadata:
labels:
app:aspire-dashboard
name:aspire-dashboard
spec:
replicas:1
selector:
matchLabels:
app:aspire-dashboard
strategy:
type:RollingUpdate
rollingUpdate:
maxSurge:1
maxUnavailable:1
template:
metadata:
labels:
app:aspire-dashboard
spec:
containers:
# https://mcr.microsoft.com/en-us/artifact/mar/dotnet/aspire-dashboard/about
-env:
-name:Authentication__Schemes__OpenIdConnect__ClientSecret
valueFrom:
secretKeyRef:
key:Aspire_ClientSecret
name:my-secrets
-name:Authentication__Schemes__OpenIdConnect__Authority
value:"https://id.weihanli.xyz"
-name:Authentication__Schemes__OpenIdConnect__ClientId
value:"aspire"
-name:Authentication__Schemes__OpenIdConnect__GetClaimsFromUserInfoEndpoint
value:"true"
-name:Dashboard__Frontend__AuthMode
value:"OpenIdConnect"
-name:"ASPNETCORE_FORWARDEDHEADERS_ENABLED"
value:"true"
image:mcr.microsoft.com/dotnet/aspire-dashboard:9.0
name:aspire-dashboard
ports:
-name:"otlp"
containerPort:18889
protocol:TCP
-name:"dashboard"
containerPort:18888
protocol:TCP
resources:
limits:
cpu:100m
memory: 256Mi