This is an automated email from the ASF dual-hosted git repository.
abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git
The following commit(s) were added to refs/heads/main by this push:
new b94ccdedf Fix zentao and opsgenis API about sensitive fields (#6686)
b94ccdedf is described below
commit b94ccdedfe27ee7087be4ea3e2784a9fc39dbaf3
Author: Lynwee <[email protected]>
AuthorDate: Fri Dec 22 10:17:49 2023 +0800
Fix zentao and opsgenis API about sensitive fields (#6686)
* fix(zentao): update zentao connection test api
* feat(opsgenie): update test connection apis
* fix(zentao): fix patch connection api with secret db url
* fix(zentao): fix bugs when patching connections
* fix(zentao): refact code
* fix(zentao): refactor code
* fix(zentao): fix lint error
---
backend/core/plugin/plugin_datasource.go | 2 +-
.../helpers/pluginhelper/api/connection_helper.go | 8 ++-
backend/plugins/opsgenie/api/connection.go | 62 +++++++++++++++-------
backend/plugins/opsgenie/impl/impl.go | 3 ++
backend/plugins/zentao/api/connection.go | 17 ++++--
backend/plugins/zentao/models/connection.go | 20 +++++++
6 files changed, 87 insertions(+), 25 deletions(-)
diff --git a/backend/core/plugin/plugin_datasource.go
b/backend/core/plugin/plugin_datasource.go
index 550aec913..d40bfc980 100644
--- a/backend/core/plugin/plugin_datasource.go
+++ b/backend/core/plugin/plugin_datasource.go
@@ -51,7 +51,7 @@ type ApiAuthenticator interface {
// TODO: deprecated, remove
// ConnectionValidator represents the API Connection would validate its fields
with customized logic
type ConnectionValidator interface {
- ValidateConnection(connection interface{}, valdator
*validator.Validate) errors.Error
+ ValidateConnection(connection interface{}, validator
*validator.Validate) errors.Error
}
// PrepareApiClient is to be implemented by the concrete Connection which
requires
diff --git a/backend/helpers/pluginhelper/api/connection_helper.go
b/backend/helpers/pluginhelper/api/connection_helper.go
index 530f03889..2179888b2 100644
--- a/backend/helpers/pluginhelper/api/connection_helper.go
+++ b/backend/helpers/pluginhelper/api/connection_helper.go
@@ -92,7 +92,6 @@ func (c *ConnectionApiHelper) Patch(connection interface{},
input *plugin.ApiRes
if err != nil {
return err
}
-
err = c.merge(connection, input.Body)
if err != nil {
return err
@@ -180,6 +179,9 @@ func (c *ConnectionApiHelper) Delete(connection
interface{}, input *plugin.ApiRe
}
return &plugin.ApiResourceOutput{Body: result}, err
}
+func (c *ConnectionApiHelper) Merge(connection interface{}, body
map[string]interface{}) errors.Error {
+ return c.merge(connection, body)
+}
func (c *ConnectionApiHelper) merge(connection interface{}, body
map[string]interface{}) errors.Error {
connection = models.UnwrapObject(connection)
@@ -193,6 +195,10 @@ func (c *ConnectionApiHelper) merge(connection
interface{}, body map[string]inte
return Decode(body, connection, c.validator)
}
+func (c *ConnectionApiHelper) SaveWithCreateOrUpdate(connection interface{})
errors.Error {
+ return c.save(connection, c.db.CreateOrUpdate)
+}
+
func (c *ConnectionApiHelper) save(connection interface{}, method func(entity
interface{}, clauses ...dal.Clause) errors.Error) errors.Error {
err := CallDB(method, connection)
if err != nil {
diff --git a/backend/plugins/opsgenie/api/connection.go
b/backend/plugins/opsgenie/api/connection.go
index 50ea2eb78..3cceb2e87 100644
--- a/backend/plugins/opsgenie/api/connection.go
+++ b/backend/plugins/opsgenie/api/connection.go
@@ -27,22 +27,7 @@ import (
"github.com/apache/incubator-devlake/plugins/opsgenie/models"
)
-// @Summary test opsgenie connection
-// @Description Test Opsgenie Connection
-// @Tags plugins/opsgenie
-// @Param body body models.OpsgenieConn true "json body"
-// @Success 200 {object} shared.ApiBody "Success"
-// @Failure 400 {string} errcode.Error "Bad Request"
-// @Failure 500 {string} errcode.Error "Internal Error"
-// @Router /plugins/opsgenie/test [POST]
-func TestConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
- var connection models.OpsgenieConn
-
- err := api.Decode(input.Body, &connection, vld)
- if err != nil {
- return nil, err
- }
-
+func testOpsgenieConn(ctx context.Context, connection models.OpsgenieConn)
(*plugin.ApiResourceOutput, errors.Error) {
apiClient, err := api.NewApiClientFromConnection(context.TODO(),
basicRes, &connection)
if err != nil {
return nil, err
@@ -73,6 +58,41 @@ func TestConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
return &plugin.ApiResourceOutput{Body: nil, Status:
response.StatusCode}, errors.HttpStatus(response.StatusCode).Wrap(err, "could
not validate connection")
}
+// TestExistingConnection test an existing opsgenie connection
+// @Summary test opsgenie connection
+// @Description Test Opsgenie Connection
+// @Tags plugins/opsgenie
+// @Success 200 {object} shared.ApiBody "Success"
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/opsgenie/{connectionId}/test [POST]
+func TestExistingConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
+ connection := models.OpsgenieConnection{}
+ err := connectionHelper.First(&connection, input.Params)
+ if err != nil {
+ return nil, err
+ }
+ return testOpsgenieConn(context.Background(), connection.OpsgenieConn)
+}
+
+// TestConnection test opsgenie connection
+// @Summary test opsgenie connection
+// @Description Test Opsgenie Connection
+// @Tags plugins/opsgenie
+// @Param body body models.OpsgenieConn true "json body"
+// @Success 200 {object} shared.ApiBody "Success"
+// @Failure 400 {string} errcode.Error "Bad Request"
+// @Failure 500 {string} errcode.Error "Internal Error"
+// @Router /plugins/opsgenie/test [POST]
+func TestConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
+ var connection models.OpsgenieConn
+ err := api.Decode(input.Body, &connection, vld)
+ if err != nil {
+ return nil, err
+ }
+ return testOpsgenieConn(context.Background(), connection)
+}
+
// @Summary create opsgenie connection
// @Description Create Opsgenie connection
// @Tags plugins/opsgenie
@@ -87,7 +107,7 @@ func PostConnections(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
if err != nil {
return nil, err
}
- return &plugin.ApiResourceOutput{Body: connection, Status:
http.StatusOK}, nil
+ return &plugin.ApiResourceOutput{Body: connection.Sanitize(), Status:
http.StatusOK}, nil
}
// @Summary patch opsgenie connection
@@ -104,7 +124,7 @@ func PatchConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
if err != nil {
return nil, err
}
- return &plugin.ApiResourceOutput{Body: connection, Status:
http.StatusOK}, nil
+ return &plugin.ApiResourceOutput{Body: connection.Sanitize(), Status:
http.StatusOK}, nil
}
// @Summary delete opsgenie connection
@@ -132,7 +152,9 @@ func ListConnections(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
if err != nil {
return nil, err
}
-
+ for idx, c := range connections {
+ connections[idx] = c.Sanitize()
+ }
return &plugin.ApiResourceOutput{Body: connections}, nil
}
@@ -149,5 +171,5 @@ func GetConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, e
if err != nil {
return nil, err
}
- return &plugin.ApiResourceOutput{Body: connection}, nil
+ return &plugin.ApiResourceOutput{Body: connection.Sanitize()}, nil
}
diff --git a/backend/plugins/opsgenie/impl/impl.go
b/backend/plugins/opsgenie/impl/impl.go
index e19c76f76..a3470da04 100644
--- a/backend/plugins/opsgenie/impl/impl.go
+++ b/backend/plugins/opsgenie/impl/impl.go
@@ -164,6 +164,9 @@ func (p Opsgenie) ApiResources()
map[string]map[string]plugin.ApiResourceHandler
"PATCH": api.PatchConnection,
"DELETE": api.DeleteConnection,
},
+ "connections/:connectionId/test": {
+ "POST": api.TestExistingConnection,
+ },
"connections/:connectionId/remote-scopes": {
"GET": api.RemoteScopes,
},
diff --git a/backend/plugins/zentao/api/connection.go
b/backend/plugins/zentao/api/connection.go
index 84168c958..2c25a2839 100644
--- a/backend/plugins/zentao/api/connection.go
+++ b/backend/plugins/zentao/api/connection.go
@@ -146,9 +146,20 @@ func PostConnections(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput,
// @Failure 500 {string} errcode.Error "Internal Error"
// @Router /plugins/zentao/connections/{connectionId} [PATCH]
func PatchConnection(input *plugin.ApiResourceInput)
(*plugin.ApiResourceOutput, errors.Error) {
- connection := &models.ZentaoConnection{}
- err := connectionHelper.Patch(connection, input)
- if err != nil {
+ existedConnection := models.ZentaoConnection{}
+ if err := connectionHelper.First(&existedConnection, input.Params); err
!= nil {
+ return nil, err
+ }
+ connection := existedConnection
+ if err := connectionHelper.Merge(connection, input.Body); err != nil {
+ return nil, err
+ }
+ // make sure zentao config's db url field is not in secret format
+ if err := (models.ZentaoConnection{}).Merge(&existedConnection,
&connection); err != nil {
+ return nil, errors.Convert(err)
+ }
+ connection.DbUrl = existedConnection.DbUrl
+ if err := connectionHelper.SaveWithCreateOrUpdate(connection); err !=
nil {
return nil, err
}
return &plugin.ApiResourceOutput{Body: connection.Sanitize()}, nil
diff --git a/backend/plugins/zentao/models/connection.go
b/backend/plugins/zentao/models/connection.go
index 9245cc1cf..fff8a1432 100644
--- a/backend/plugins/zentao/models/connection.go
+++ b/backend/plugins/zentao/models/connection.go
@@ -74,6 +74,7 @@ func (connection ZentaoConn) Sanitize() ZentaoConn {
if connection.DbUrl != "" {
connection.DbUrl = connection.SanitizeDbUrl()
}
+ connection.Password = ""
return connection
}
@@ -106,6 +107,25 @@ func (connection ZentaoConnection) Sanitize()
ZentaoConnection {
return connection
}
+// Merge works with the new connection helper.
+func (connection ZentaoConnection) Merge(existed, modified *ZentaoConnection)
error {
+ existedDBUrl := existed.DbUrl
+ if existedDBUrl != "" && modified.DbUrl != "" {
+ existedSanitizedConnection := existed.Sanitize()
+ if existedSanitizedConnection.DbUrl != modified.DbUrl {
+ // db url is updated
+ existed.DbUrl = modified.DbUrl
+ } else {
+ // there is no change with db url field.
+ // existedDBUrl = origin(modified.DbUrl)
+ return nil
+ }
+ } else {
+ existed.DbUrl = modified.DbUrl
+ }
+ return nil
+}
+
// This object conforms to what the frontend currently expects.
type ZentaoResponse struct {
Name string `json:"name"`