Skip to content

Commit d581390

Browse files
committed
feat(token): add get token by uuid api
1 parent b956bfd commit d581390

3 files changed

Lines changed: 20 additions & 7 deletions

File tree

charts/controller/templates/_helpers.tpl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,18 @@ env:
119119
name: controller-creds
120120
key: victoriametrics-url
121121
{{- else if .Values.victoriametrics.enabled }}
122+
- name: "DRYCC_VICTORIAMETRICS_USERNAME"
123+
valueFrom:
124+
secretKeyRef:
125+
name: victoriametrics-vmauth-creds
126+
key: username
127+
- name: "DRYCC_VICTORIAMETRICS_PASSWORD"
128+
valueFrom:
129+
secretKeyRef:
130+
name: victoriametrics-vmauth-creds
131+
key: password
122132
- name: "DRYCC_VICTORIAMETRICS_URL"
123-
value: "http://drycc-victoriametrics-vmselect.{{$.Release.Namespace}}.svc.{{$.Values.global.clusterDomain}}:8481"
133+
value: "http://$(DRYCC_VICTORIAMETRICS_USERNAME):$(DRYCC_VICTORIAMETRICS_PASSWORD)@drycc-victoriametrics-vmauth.{{$.Release.Namespace}}.svc.{{$.Values.global.clusterDomain}}:8481"
124134
{{- end }}
125135
{{- if .Values.passport.enabled }}
126136
- name: "DRYCC_PASSPORT_URL"

rootfs/api/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@
253253
r'^prometheus/(?P<username>[\w.@+-]+)/(?P<path>.+)/?$', views.PrometheusProxy.as_view()),
254254
# tokens
255255
re_path(r'^tokens/?$', views.TokenViewSet.as_view({'get': 'list'})),
256-
re_path(r"^tokens/(?P<pk>[-_\w]+)/?$", views.TokenViewSet.as_view({'delete': 'destroy'})),
256+
re_path(
257+
r"^tokens/(?P<pk>[-_\w]+)/?$",
258+
views.TokenViewSet.as_view({'get': 'retrieve', 'delete': 'destroy'})),
257259
]
258260

259261
mutate_urlpatterns = [

rootfs/api/views.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ def token(self, request, *args, **kwargs):
156156
alias = request.query_params.get('alias', '')
157157
token = models.base.Token(owner=user, alias=alias, oauth=oauth)
158158
token.save()
159-
return HttpResponse(json.dumps({"token": token.key, "username": user.username}))
159+
return HttpResponse(json.dumps(
160+
{"uuid": str(token.uuid), "token": token.key, "username": user.username}))
160161
return HttpResponse(status=404)
161162

162163

@@ -1338,10 +1339,10 @@ class PrometheusProxy(View):
13381339

13391340
async def proxy(self, request, username, path):
13401341
auth = await database_sync_to_async(self.authentication.authenticate)(request)
1341-
if not auth or len(auth) != 2 or not auth[0].username != username:
1342-
return JsonResponse({'error': 'access denied'}, status=403)
1343-
if auth[0].is_superuser or auth[0].is_staff:
1344-
path = f"/select/0/prometheus/{path}"
1342+
if not auth or len(auth) != 2:
1343+
return JsonResponse({'error': 'Unauthorized'}, status=401)
1344+
if auth[0].username != username:
1345+
return JsonResponse({'error': 'Access denied'}, status=403)
13451346
else:
13461347
path = f"/select/{auth[0].id}/prometheus/{path}"
13471348
url = urljoin(settings.DRYCC_VICTORIAMETRICS_URL, path)

0 commit comments

Comments
 (0)