11package main
22
33import (
4- "crypto/aes"
5- "crypto/cipher"
6- "crypto/sha1"
7- "encoding/base64"
84 "fmt"
95 "os"
106 "path/filepath"
@@ -14,13 +10,14 @@ import (
1410 "github.com/fsnotify/fsnotify"
1511 "github.com/threatwinds/go-sdk/catcher"
1612 "github.com/threatwinds/go-sdk/plugins"
17- "golang.org/x/crypto/pbkdf2 "
13+ "github.com/utmstack/UTMStack/plugins/shared/crypto "
1814 "gopkg.in/yaml.v3"
1915)
2016
2117const (
2218 pluginFile = "system_plugins_aws.yaml"
23- processName = "plugin_com.utmstack.aws"
19+ pluginName = "com.utmstack.aws"
20+ processName = "plugin_" + pluginName
2421 pipelineDirDefault = "/workdir/pipeline"
2522)
2623
@@ -84,7 +81,6 @@ func StartConfigurationSystem() {
8481
8582 filePath := filepath .Join (pipelineDir , pluginFile )
8683
87- // Initial load.
8884 if sec := readConfig (filePath , encKey ); sec != nil {
8985 mu .Lock ()
9086 cnf = sec
@@ -100,7 +96,7 @@ func StartConfigurationSystem() {
10096 }
10197 defer watcher .Close ()
10298
103- // Watch the directory so we catch atomic write (rename) events .
99+ // The directory, not the file: atomic writes arrive as renames .
104100 if err := watcher .Add (pipelineDir ); err != nil {
105101 _ = catcher .Error ("failed to watch pipeline dir" , err , map [string ]any {"process" : processName })
106102 pollFallback (filePath , encKey )
@@ -182,8 +178,7 @@ func readConfig(path, encKey string) *ConfigurationSection {
182178 data , err := os .ReadFile (path )
183179 if err != nil {
184180 if os .IsNotExist (err ) {
185- // File removed → module disabled / no configuration. Report an empty,
186- // inactive section so the module is treated as disabled and all work stops.
181+ // A removed file means the module is disabled, not an error.
187182 return & ConfigurationSection {ModuleActive : false }
188183 }
189184 _ = catcher .Error ("failed to read config file" , err , map [string ]any {"process" : processName , "file" : path })
@@ -210,7 +205,7 @@ func readConfig(path, encKey string) *ConfigurationSection {
210205 for k , v := range g .Config {
211206 conf := & Configuration {ConfKey : k , ConfValue : v }
212207 if encKey != "" && sensitiveKeys [k ] {
213- dec , err := NewCipher (encKey ).Decrypt (conf .ConfValue )
208+ dec , err := crypto . NewCipher (encKey ).Decrypt (conf .ConfValue )
214209 if err == nil {
215210 conf .ConfValue = dec
216211 }
@@ -220,64 +215,6 @@ func readConfig(path, encKey string) *ConfigurationSection {
220215 sec .ModuleGroups = append (sec .ModuleGroups , grp )
221216 }
222217 }
223- // A tenant section with no groups must not read as configured.
224218 sec .ModuleActive = len (sec .ModuleGroups ) > 0
225219 return sec
226220}
227-
228- const (
229- iterationCount = 65536
230- keyLength = 16
231- )
232-
233- type Cipher struct {
234- key []byte
235- }
236-
237- func NewCipher (key string ) * Cipher {
238- return & Cipher {key : []byte (key )}
239- }
240-
241- func (c * Cipher ) setKey () (cipher.Block , []byte , error ) {
242- h := sha1 .New ()
243- h .Write (c .key )
244- salt := h .Sum (nil )
245- keyEnc := pbkdf2 .Key (c .key , salt , iterationCount , keyLength , sha1 .New )
246- block , err := aes .NewCipher (keyEnc )
247- if err != nil {
248- return nil , nil , err
249- }
250- return block , salt [:keyLength ], nil
251- }
252-
253- func (c * Cipher ) Decrypt (crypt string ) (string , error ) {
254- if crypt == "" {
255- return "" , nil
256- }
257- encryptedData , err := base64 .StdEncoding .DecodeString (crypt )
258- if err != nil {
259- return crypt , nil // not base64 → already plaintext
260- }
261- blk , iv , err := c .setKey ()
262- if err != nil {
263- return crypt , err
264- }
265- if len (encryptedData )% aes .BlockSize != 0 {
266- return crypt , nil // not a valid CBC block → already plaintext
267- }
268- dec := cipher .NewCBCDecrypter (blk , iv )
269- decrypted := make ([]byte , len (encryptedData ))
270- dec .CryptBlocks (decrypted , encryptedData )
271- return string (pkcs5Trim (decrypted )), nil
272- }
273-
274- func pkcs5Trim (data []byte ) []byte {
275- if len (data ) == 0 {
276- return data
277- }
278- padding := int (data [len (data )- 1 ])
279- if padding > len (data ) || padding == 0 {
280- return data
281- }
282- return data [:len (data )- padding ]
283- }
0 commit comments