|
| 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 | +} |
0 commit comments