Skip to content

Commit 0564d56

Browse files
feat: cluster bearer token hide from dashboard (#2894) (#11)
* cluster token config removed mandatory * api spec added for cluster update and create, and cluster list api changes for token * fix check config for cluster token
1 parent aeb233c commit 0564d56

File tree

4 files changed

+163
-29
lines changed

4 files changed

+163
-29
lines changed

api/cluster/ClusterRestHandler.go

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const CLUSTER_DELETE_SUCCESS_RESP = "Cluster deleted successfully."
4343

4444
type ClusterRestHandler interface {
4545
Save(w http.ResponseWriter, r *http.Request)
46-
FindOne(w http.ResponseWriter, r *http.Request)
4746
FindAll(w http.ResponseWriter, r *http.Request)
4847

4948
FindById(w http.ResponseWriter, r *http.Request)
@@ -153,29 +152,9 @@ func (impl ClusterRestHandlerImpl) Save(w http.ResponseWriter, r *http.Request)
153152
common.WriteJsonResp(w, err, bean, http.StatusOK)
154153
}
155154

156-
func (impl ClusterRestHandlerImpl) FindOne(w http.ResponseWriter, r *http.Request) {
157-
vars := mux.Vars(r)
158-
cName := vars["cluster_name"]
159-
// RBAC enforcer applying
160-
token := r.Header.Get("token")
161-
if ok := impl.enforcer.Enforce(token, casbin.ResourceCluster, casbin.ActionGet, strings.ToLower(cName)); !ok {
162-
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
163-
return
164-
}
165-
//RBAC enforcer Ends
166-
167-
envBean, err := impl.clusterService.FindOne(cName)
168-
if err != nil {
169-
impl.logger.Errorw("service err, FindOne", "error", err, "cluster name", cName)
170-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
171-
return
172-
}
173-
common.WriteJsonResp(w, err, envBean, http.StatusOK)
174-
}
175-
176155
func (impl ClusterRestHandlerImpl) FindAll(w http.ResponseWriter, r *http.Request) {
177156
token := r.Header.Get("token")
178-
clusterList, err := impl.clusterService.FindAll()
157+
clusterList, err := impl.clusterService.FindAllWithoutConfig()
179158
if err != nil {
180159
impl.logger.Errorw("service err, FindAll", "err", err)
181160
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -203,7 +182,7 @@ func (impl ClusterRestHandlerImpl) FindById(w http.ResponseWriter, r *http.Reque
203182
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
204183
return
205184
}
206-
bean, err := impl.clusterService.FindById(i)
185+
bean, err := impl.clusterService.FindByIdWithoutConfig(i)
207186
if err != nil {
208187
impl.logger.Errorw("service err, FindById", "err", err, "clusterId", id)
209188
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)

pkg/cluster/ClusterService.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type ClusterBean struct {
5050
ServerUrl string `json:"server_url,omitempty" validate:"url,required"`
5151
PrometheusUrl string `json:"prometheus_url,omitempty" validate:"validate-non-empty-url"`
5252
Active bool `json:"active"`
53-
Config map[string]string `json:"config,omitempty" validate:"required"`
53+
Config map[string]string `json:"config,omitempty"`
5454
PrometheusAuth *PrometheusAuth `json:"prometheusAuth,omitempty"`
5555
DefaultClusterComponent []*DefaultClusterComponent `json:"defaultClusterComponent"`
5656
AgentInstallationStage int `json:"agentInstallationStage,notnull"` // -1=external, 0=not triggered, 1=progressing, 2=success, 3=fails
@@ -80,10 +80,12 @@ type ClusterService interface {
8080
FindOne(clusterName string) (*ClusterBean, error)
8181
FindOneActive(clusterName string) (*ClusterBean, error)
8282
FindAll() ([]*ClusterBean, error)
83+
FindAllWithoutConfig() ([]*ClusterBean, error)
8384
FindAllActive() ([]ClusterBean, error)
8485
DeleteFromDb(bean *ClusterBean, userId int32) error
8586

8687
FindById(id int) (*ClusterBean, error)
88+
FindByIdWithoutConfig(id int) (*ClusterBean, error)
8789
FindByIds(id []int) ([]ClusterBean, error)
8890
Update(ctx context.Context, bean *ClusterBean, userId int32) (*ClusterBean, error)
8991
Delete(bean *ClusterBean, userId int32) error
@@ -257,6 +259,17 @@ func (impl *ClusterServiceImpl) FindOneActive(clusterName string) (*ClusterBean,
257259
return bean, nil
258260
}
259261

262+
func (impl *ClusterServiceImpl) FindAllWithoutConfig() ([]*ClusterBean, error) {
263+
models, err := impl.FindAll()
264+
if err != nil {
265+
return nil, err
266+
}
267+
for _, model := range models {
268+
model.Config = map[string]string{"bearer_token": ""}
269+
}
270+
return models, nil
271+
}
272+
260273
func (impl *ClusterServiceImpl) FindAll() ([]*ClusterBean, error) {
261274
model, err := impl.clusterRepository.FindAllActive()
262275
if err != nil {
@@ -326,6 +339,16 @@ func (impl *ClusterServiceImpl) FindById(id int) (*ClusterBean, error) {
326339
return bean, nil
327340
}
328341

342+
func (impl *ClusterServiceImpl) FindByIdWithoutConfig(id int) (*ClusterBean, error) {
343+
model, err := impl.FindById(id)
344+
if err != nil {
345+
return nil, err
346+
}
347+
//empty bearer token as it will be hidden for user
348+
model.Config = map[string]string{"bearer_token": ""}
349+
return model, nil
350+
}
351+
329352
func (impl *ClusterServiceImpl) FindByIds(ids []int) ([]ClusterBean, error) {
330353
models, err := impl.clusterRepository.FindByIds(ids)
331354
if err != nil {
@@ -349,11 +372,6 @@ func (impl *ClusterServiceImpl) FindByIds(ids []int) ([]ClusterBean, error) {
349372
}
350373

351374
func (impl *ClusterServiceImpl) Update(ctx context.Context, bean *ClusterBean, userId int32) (*ClusterBean, error) {
352-
//validating config
353-
err := impl.CheckIfConfigIsValid(bean)
354-
if err != nil {
355-
return nil, err
356-
}
357375
model, err := impl.clusterRepository.FindById(bean.Id)
358376
if err != nil {
359377
impl.logger.Error(err)
@@ -372,8 +390,16 @@ func (impl *ClusterServiceImpl) Update(ctx context.Context, bean *ClusterBean, u
372390
// check whether config modified or not, if yes create informer with updated config
373391
dbConfig := model.Config["bearer_token"]
374392
requestConfig := bean.Config["bearer_token"]
393+
if len(requestConfig) == 0 {
394+
bean.Config = model.Config
395+
}
375396
if bean.ServerUrl != model.ServerUrl || dbConfig != requestConfig {
376397
bean.HasConfigOrUrlChanged = true
398+
//validating config
399+
err := impl.CheckIfConfigIsValid(bean)
400+
if err != nil {
401+
return nil, err
402+
}
377403
}
378404
model.ClusterName = bean.ClusterName
379405
model.ServerUrl = bean.ServerUrl

pkg/cluster/ClusterServiceExtended.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ func NewClusterServiceImplExtended(repository repository.ClusterRepository, envi
5959
return clusterServiceExt
6060
}
6161

62+
func (impl *ClusterServiceImplExtended) FindAllWithoutConfig() ([]*ClusterBean, error) {
63+
beans, err := impl.FindAll()
64+
if err != nil {
65+
return nil, err
66+
}
67+
for _, bean := range beans {
68+
bean.Config = map[string]string{"bearer_token": ""}
69+
}
70+
return beans, nil
71+
}
72+
6273
func (impl *ClusterServiceImplExtended) FindAll() ([]*ClusterBean, error) {
6374
beans, err := impl.ClusterServiceImpl.FindAll()
6475
if err != nil {

specs/cluster_api_spec.yaml

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,77 @@ info:
33
version: 1.0.0
44
title: Devtron Labs
55
paths:
6+
/orchestrator/cluster:
7+
put:
8+
description: Update Cluster
9+
operationId: UpdateCluster
10+
requestBody:
11+
description: A JSON object containing the cluster config
12+
required: true
13+
content:
14+
application/json:
15+
schema:
16+
$ref: '#/components/schemas/ClusterBean'
17+
responses:
18+
'200':
19+
description: Successfully update the cluster
20+
content:
21+
application/json:
22+
schema:
23+
$ref: '#/components/schemas/ClusterBean'
24+
'400':
25+
description: Bad Request. Input Validation(decode) error/wrong request body.
26+
content:
27+
application/json:
28+
schema:
29+
$ref: '#/components/schemas/Error'
30+
'500':
31+
description: Internal Server Error
32+
content:
33+
application/json:
34+
schema:
35+
$ref: '#/components/schemas/Error'
36+
'401':
37+
description: Unauthorized User
38+
content:
39+
application/json:
40+
schema:
41+
$ref: '#/components/schemas/Error'
42+
get:
43+
description: Get Cluster
44+
operationId: GetCluster
45+
parameters:
46+
- name: id
47+
in: query
48+
description: cluster id.
49+
required: true
50+
schema:
51+
type: integer
52+
responses:
53+
'200':
54+
description: Successfully get cluster
55+
content:
56+
application/json:
57+
schema:
58+
$ref: '#/components/schemas/ClusterBean'
59+
'400':
60+
description: Bad Request. Input Validation(decode) error/wrong request body.
61+
content:
62+
application/json:
63+
schema:
64+
$ref: '#/components/schemas/Error'
65+
'500':
66+
description: Internal Server Error
67+
content:
68+
application/json:
69+
schema:
70+
$ref: '#/components/schemas/Error'
71+
'401':
72+
description: Unauthorized User
73+
content:
74+
application/json:
75+
schema:
76+
$ref: '#/components/schemas/Error'
677
/orchestrator/cluster/auth-list:
778
get:
879
description: list of accessible cluster
@@ -34,6 +105,53 @@ paths:
34105
# components mentioned below
35106
components:
36107
schemas:
108+
ClusterBean:
109+
type: object
110+
properties:
111+
id:
112+
type: integer
113+
cluster_name:
114+
type: string
115+
server_url:
116+
type: string
117+
prometheus_url:
118+
type: string
119+
active:
120+
type: boolean
121+
config:
122+
type: object
123+
properties:
124+
bearer_token:
125+
type: string
126+
description: it will be empty while fetching, and if no change while updating
127+
k8sversion:
128+
type: string
129+
PrometheusAuth:
130+
type: object
131+
properties:
132+
userName:
133+
type: string
134+
password:
135+
type: string
136+
tlsClientCert:
137+
type: string
138+
tlsClientKey:
139+
type: string
140+
DefaultClusterComponent:
141+
type: object
142+
properties:
143+
name:
144+
type: string
145+
appId:
146+
type: integer
147+
installedAppId:
148+
type: integer
149+
envId:
150+
type: integer
151+
envname:
152+
type: string
153+
status:
154+
type: string
37155
Cluster:
38156
type: object
39157
required:

0 commit comments

Comments
 (0)