Skip to content

Commit c413ebc

Browse files
authored
fix(asana): implement encryption for Asana connection token (#8873)
1 parent ecb703c commit c413ebc

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

backend/plugins/asana/models/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// AsanaConn holds the essential information to connect to the Asana API
3030
type AsanaConn struct {
3131
helper.RestConnection `mapstructure:",squash"`
32-
Token string `mapstructure:"token" json:"token" encrypt:"yes"`
32+
Token string `mapstructure:"token" json:"token" gorm:"serializer:encdec"`
3333
}
3434

3535
func (ac *AsanaConn) Sanitize() AsanaConn {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one or more
3+
contributor license agreements. See the NOTICE file distributed with
4+
this work for additional information regarding copyright ownership.
5+
The ASF licenses this file to You under the Apache License, Version 2.0
6+
(the "License"); you may not use this file except in compliance with
7+
the License. You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
package migrationscripts
19+
20+
import (
21+
"github.com/apache/incubator-devlake/core/context"
22+
"github.com/apache/incubator-devlake/core/dal"
23+
"github.com/apache/incubator-devlake/core/errors"
24+
"github.com/apache/incubator-devlake/core/plugin"
25+
)
26+
27+
type asanaConnectionTokenPlain struct {
28+
ID uint64 `gorm:"primaryKey"`
29+
Token string
30+
}
31+
32+
func (asanaConnectionTokenPlain) TableName() string {
33+
return "_tool_asana_connections"
34+
}
35+
36+
type encryptConnectionToken struct{}
37+
38+
func (*encryptConnectionToken) Up(basicRes context.BasicRes) errors.Error {
39+
db := basicRes.GetDal()
40+
encKey := basicRes.GetConfig(plugin.EncodeKeyEnvStr)
41+
if encKey == "" {
42+
return errors.BadInput.New("asana invalid encKey")
43+
}
44+
45+
cursor, err := db.Cursor(dal.From(&asanaConnectionTokenPlain{}))
46+
if err != nil {
47+
return err
48+
}
49+
defer cursor.Close()
50+
51+
for cursor.Next() {
52+
row := &asanaConnectionTokenPlain{}
53+
if err = db.Fetch(cursor, row); err != nil {
54+
return err
55+
}
56+
if row.Token == "" {
57+
continue
58+
}
59+
encryptedToken, err := plugin.Encrypt(encKey, row.Token)
60+
if err != nil {
61+
return err
62+
}
63+
err = db.UpdateColumns(
64+
row.TableName(),
65+
[]dal.DalSet{{ColumnName: "token", Value: encryptedToken}},
66+
dal.Where("id = ?", row.ID),
67+
)
68+
if err != nil {
69+
return err
70+
}
71+
}
72+
73+
return nil
74+
}
75+
76+
func (*encryptConnectionToken) Version() uint64 {
77+
return 20260509000001
78+
}
79+
80+
func (*encryptConnectionToken) Name() string {
81+
return "encrypt asana connection token"
82+
}

backend/plugins/asana/models/migrationscripts/register.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ func All() []plugin.MigrationScript {
3030
new(addTaskTransformationFields),
3131
new(addScopeConfigIssueTypeFields),
3232
new(addConnectionIdToAsanaScopeConfigs),
33+
new(encryptConnectionToken),
3334
}
3435
}

0 commit comments

Comments
 (0)