Skip to content

Commit 8a5e234

Browse files
committed
Add HIDE_API_TOKENS config and pass apiTokenConfig to ApiTokenServiceImpl to make token visibility configurable
1 parent 5626600 commit 8a5e234

File tree

6 files changed

+39
-7
lines changed

6 files changed

+39
-7
lines changed

api/apiToken/wire_apiToken.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var ApiTokenWireSet = wire.NewSet(
2626
wire.Bind(new(apiToken.ApiTokenRepository), new(*apiToken.ApiTokenRepositoryImpl)),
2727
apiToken.NewApiTokenServiceImpl,
2828
wire.Bind(new(apiToken.ApiTokenService), new(*apiToken.ApiTokenServiceImpl)),
29+
apiToken.GetApiTokenConfig,
2930
NewApiTokenRestHandlerImpl,
3031
wire.Bind(new(ApiTokenRestHandler), new(*ApiTokenRestHandlerImpl)),
3132
NewApiTokenRouterImpl,

cmd/external-app/wire_gen.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

env_gen.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

env_gen.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@
200200
| GRAFANA_PORT | string |8090 | Port for grafana micro-service | | false |
201201
| GRAFANA_URL | string | | Host URL for the grafana dashboard | | false |
202202
| GRAFANA_USERNAME | string |admin | Username for grafana | | false |
203+
| HIDE_API_TOKENS | bool |false | Boolean flag for should the api tokens generated be hidden from the UI | | false |
203204
| HIDE_IMAGE_TAGGING_HARD_DELETE | bool |false | Flag to hide the hard delete option in the image tagging service | | false |
204205
| IGNORE_AUTOCOMPLETE_AUTH_CHECK | bool |false | flag for ignoring auth check in autocomplete apis. | | false |
205206
| INSTALLED_MODULES | | | List of installed modules given in helm values/yaml are written in cm and used by devtron to know which modules are given | security.trivy,security.clair | false |

pkg/apiToken/ApiTokenService.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package apiToken
1919
import (
2020
"errors"
2121
"fmt"
22+
"github.com/caarlos0/env"
2223
userBean "github.com/devtron-labs/devtron/pkg/auth/user/bean"
2324
"regexp"
2425
"strconv"
@@ -48,19 +49,36 @@ type ApiTokenServiceImpl struct {
4849
userService user2.UserService
4950
userAuditService user2.UserAuditService
5051
apiTokenRepository ApiTokenRepository
52+
apiTokenConfig *ApiTokenConfig
5153
}
5254

53-
func NewApiTokenServiceImpl(logger *zap.SugaredLogger, apiTokenSecretService ApiTokenSecretService, userService user2.UserService, userAuditService user2.UserAuditService,
54-
apiTokenRepository ApiTokenRepository) *ApiTokenServiceImpl {
55+
func NewApiTokenServiceImpl(logger *zap.SugaredLogger,
56+
apiTokenSecretService ApiTokenSecretService,
57+
userService user2.UserService,
58+
userAuditService user2.UserAuditService,
59+
apiTokenRepository ApiTokenRepository,
60+
apiTokenConfig *ApiTokenConfig,
61+
) *ApiTokenServiceImpl {
5562
return &ApiTokenServiceImpl{
5663
logger: logger,
5764
apiTokenSecretService: apiTokenSecretService,
5865
userService: userService,
5966
userAuditService: userAuditService,
6067
apiTokenRepository: apiTokenRepository,
68+
apiTokenConfig: apiTokenConfig,
6169
}
6270
}
6371

72+
type ApiTokenConfig struct {
73+
HideApiTokens bool `env:"HIDE_API_TOKENS" envDefault:"false" description:"Boolean flag for should the api tokens generated be hidden from the UI"`
74+
}
75+
76+
func GetApiTokenConfig() (*ApiTokenConfig, error) {
77+
cfg := &ApiTokenConfig{}
78+
err := env.Parse(cfg)
79+
return cfg, err
80+
}
81+
6482
var invalidCharsInApiTokenName = regexp.MustCompile("[,\\s]")
6583

6684
const (
@@ -104,9 +122,11 @@ func (impl ApiTokenServiceImpl) GetAllApiTokensForWebhook(projectName string, en
104122
Name: &apiTokenFromDb.Name,
105123
Description: &apiTokenFromDb.Description,
106124
ExpireAtInMs: &apiTokenFromDb.ExpireAtInMs,
107-
Token: &apiTokenFromDb.Token,
108125
UpdatedAt: &updatedAtStr,
109126
}
127+
if !impl.apiTokenConfig.HideApiTokens {
128+
apiToken.Token = &apiTokenFromDb.Token
129+
}
110130
apiTokens = append(apiTokens, apiToken)
111131
}
112132
}
@@ -140,9 +160,11 @@ func (impl ApiTokenServiceImpl) GetAllActiveApiTokens() ([]*openapi.ApiToken, er
140160
Name: &apiTokenFromDb.Name,
141161
Description: &apiTokenFromDb.Description,
142162
ExpireAtInMs: &apiTokenFromDb.ExpireAtInMs,
143-
Token: &apiTokenFromDb.Token,
144163
UpdatedAt: &updatedAtStr,
145164
}
165+
if !impl.apiTokenConfig.HideApiTokens {
166+
apiToken.Token = &apiTokenFromDb.Token
167+
}
146168
if latestAuditLog != nil {
147169
lastUsedAtStr := latestAuditLog.CreatedOn.String()
148170
apiToken.LastUsedAt = &lastUsedAtStr

wire_gen.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)