octopush/internal/services/signer/openpgp.go

64 lines
1.4 KiB
Go
Raw Normal View History

2022-09-12 14:38:15 +02:00
package signer
import (
"context"
"os"
"git.front.kjuulh.io/kjuulh/curre"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"go.uber.org/zap"
)
type OpenPGP struct {
logger *zap.Logger
PrivateKeyRing *crypto.KeyRing
config *OpenPgpConfig
}
type OpenPgpConfig struct {
PrivateKeyFilePath string
PrivateKeyPassword string
}
func NewOpenPGP(logger *zap.Logger, config *OpenPgpConfig) *OpenPGP {
return &OpenPGP{
logger: logger,
config: config,
}
}
func NewOpenPGPApp(openPGP *OpenPGP) curre.Component {
return curre.NewFunctionalComponent(&curre.FunctionalComponent{
InitFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
content, err := os.ReadFile(openPGP.config.PrivateKeyFilePath)
if err != nil {
return err
}
privateKeyObj, err := crypto.NewKeyFromArmored(string(content))
if err != nil {
return err
}
unlockedPrivateKeyRing, err := privateKeyObj.Unlock([]byte(openPGP.config.PrivateKeyPassword))
if err != nil {
return err
}
privateKeyRing, err := crypto.NewKeyRing(unlockedPrivateKeyRing)
if err != nil {
return err
}
openPGP.PrivateKeyRing = privateKeyRing
return nil
},
StartFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
return nil
},
StopFunc: func(fc *curre.FunctionalComponent, ctx context.Context) error {
return nil
},
})
}