Skip to content

Commit

Permalink
Merge pull request #43 from wttech/custom-password
Browse files Browse the repository at this point in the history
Instance custom passwords PoC
  • Loading branch information
krystian-panek-vmltech authored Jan 26, 2023
2 parents 04909e7 + 5a53833 commit 04506d6
Show file tree
Hide file tree
Showing 26 changed files with 562 additions and 200 deletions.
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,14 @@

# Force UNIX line-ending to avoid errors on higher environments
* text=auto eol=lf

# Handle embedded binaries as Git LFS resources

**/resource/*.zip filter=lfs diff=lfs merge=lfs -text
**/resource/*.tar.gz filter=lfs diff=lfs merge=lfs -text
**/resource/*.tar filter=lfs diff=lfs merge=lfs -text
**/resource/*.jar filter=lfs diff=lfs merge=lfs -text
**/resource/*.exe filter=lfs diff=lfs merge=lfs -text
**/resource/*.rpm filter=lfs diff=lfs merge=lfs -text
**/resource/*.deb filter=lfs diff=lfs merge=lfs -text
**/resource/*.so filter=lfs diff=lfs merge=lfs -text
12 changes: 0 additions & 12 deletions .run/instance_create.run.xml

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/aem/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
)

const (
OutputFileDefault = common.HomeDir + "/aem.log"
OutputFileDefault = common.LogDir + "/aem.log"
)

type CLI struct {
Expand Down
10 changes: 5 additions & 5 deletions cmd/aem/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"github.com/spf13/cobra"
"github.com/wttech/aemc/pkg/cfg"
)
Expand Down Expand Up @@ -32,12 +31,13 @@ func (c *CLI) configListCmd() *cobra.Command {

func (c *CLI) configInitCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize configuration",
Use: "initialize",
Aliases: []string{"init"},
Short: "Initialize configuration",
Run: func(cmd *cobra.Command, args []string) {
err := c.config.Init()
err := c.config.Initialize()
if err != nil {
c.Fail(fmt.Sprintf("cannot initialize config: %s", err))
c.Error(err)
return
}
c.SetOutput("path", cfg.File())
Expand Down
42 changes: 42 additions & 0 deletions cmd/aem/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"github.com/spf13/cobra"
"github.com/wttech/aemc/pkg/cfg"
"github.com/wttech/aemc/pkg/common"
"strings"
)

func (c *CLI) initCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "initialize",
Aliases: []string{"init"},
Short: "Initializes configuration and dependencies",
Run: func(cmd *cobra.Command, args []string) {
if !c.config.IsInitialized() {
if err := c.config.Initialize(); err != nil {
c.Error(err)
return
}
}
c.SetOutput("gettingStarted", strings.Join([]string{
"The next step is providing AEM files (JAR or SDK ZIP, license) to directory '" + common.LibDir + "'.",
"Alternatively, instruct the tool where these files are located by adjusting properties: 'dist_file', 'license_file' in configuration file '" + cfg.FileDefault + "'.",
"To avoid problems with IDE performance, make sure to exclude from indexing the directory '" + common.HomeDir + "'.",
"Finally, use control scripts to manage AEM instances:",
"",

"sh aemw [setup|resetup|up|down|restart]",

"",
"It is also possible to run individual AEM Compose CLI commands separately.",
"Discover available commands by running:",
"",

"sh aemw --help",
}, "\n"))
c.Ok("initialized properly")
},
}
return cmd
}
1 change: 1 addition & 0 deletions cmd/aem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (c *CLI) rootCmd() *cobra.Command {
}
cmd.AddCommand(c.appCmd())
cmd.AddCommand(c.versionCmd())
cmd.AddCommand(c.initCmd())
cmd.AddCommand(c.configCmd())
cmd.AddCommand(c.instanceCmd())
cmd.AddCommand(c.osgiCmd())
Expand Down
73 changes: 48 additions & 25 deletions pkg/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cfg

import (
"bytes"
_ "embed"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand All @@ -18,11 +17,13 @@ import (
)

const (
EnvPrefix = "AEM"
InputStdin = "STDIN"
OutputFileDefault = common.HomeDir + "/aem.log"
FilePathDefault = common.HomeDir + "/aem.yml"
FilePathEnvVar = "AEM_CONFIG_FILE"
EnvPrefix = "AEM"
InputStdin = "STDIN"
OutputFileDefault = common.LogDir + "/aem.log"
FileDefault = common.ConfigDir + "/aem.yml"
FileEnvVar = "AEM_CONFIG_FILE"
TemplateFileDefault = common.DefaultDir + "/" + common.ConfigDirName + "/aem.yml"
TemplateFileEnvVar = "AEM_CONFIG_TEMPLATE"
)

// Config defines a place for managing input configuration from various sources (YML file, env vars, etc)
Expand Down Expand Up @@ -102,37 +103,48 @@ func readFromFile(v *viper.Viper) {
}

func File() string {
path := os.Getenv(FilePathEnvVar)
if len(path) == 0 {
path = FilePathDefault
path := os.Getenv(FileEnvVar)
if path == "" {
path = FileDefault
}
return path
}

func (c *Config) ConfigureLogger() {
log.SetFormatter(&log.TextFormatter{
TimestampFormat: c.values.Log.TimestampFormat,
FullTimestamp: c.values.Log.FullTimestamp,
})

level, err := log.ParseLevel(c.values.Log.Level)
if err != nil {
log.Fatalf("unsupported log level specified: '%s'", c.values.Log.Level)
func TemplateFile() string {
path := os.Getenv(TemplateFileEnvVar)
if path == "" {
path = TemplateFileDefault
}
log.SetLevel(level)
return path
}

//go:embed aem.yml
var configYml string
func (c *Config) IsInitialized() bool {
return pathx.Exists(File())
}

func (c *Config) Init() error {
func (c *Config) Initialize() error {
file := File()
if pathx.Exists(file) {
return fmt.Errorf("config file already exists: '%s'", file)
}
err := filex.WriteString(file, configYml)
templateFile := TemplateFile()
if !pathx.Exists(templateFile) {
return fmt.Errorf("config file template does not exist: '%s'", templateFile)
}
ymlTplStr, err := filex.ReadString(templateFile)
if err != nil {
return err
}
ymlTplParsed, err := tplx.New("config-tpl").Delims("[[", "]]").Parse(ymlTplStr)
if err != nil {
return fmt.Errorf("cannot create initial config file '%s': '%w'", file, err)
return fmt.Errorf("cannot parse config file template '%s': '%w'", templateFile, err)
}
var yml bytes.Buffer
if err := ymlTplParsed.Execute(&yml, map[string]any{ /* TODO future hook (not sure if needed or not */ }); err != nil {
return fmt.Errorf("cannot render config file template '%s': '%w'", templateFile, err)
}
if err = filex.WriteString(file, yml.String()); err != nil {
return fmt.Errorf("cannot save config file '%s': '%w'", file, err)
}
return nil
}
Expand All @@ -141,7 +153,18 @@ func InputFormats() []string {
return []string{fmtx.YML, fmtx.JSON}
}

// OutputFormats returns all available output formats
func OutputFormats() []string {
return []string{fmtx.Text, fmtx.YML, fmtx.JSON, fmtx.None}
}

func (c *Config) ConfigureLogger() {
log.SetFormatter(&log.TextFormatter{
TimestampFormat: c.values.Log.TimestampFormat,
FullTimestamp: c.values.Log.FullTimestamp,
})
level, err := log.ParseLevel(c.values.Log.Level)
if err != nil {
log.Fatalf("unsupported log level specified: '%s'", c.values.Log.Level)
}
log.SetLevel(level)
}
5 changes: 5 additions & 0 deletions pkg/cfg/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ type ConfigValues struct {

Local struct {
UnpackDir string `mapstructure:"unpack_dir" yaml:"unpack_dir"`
ToolDir string `mapstructure:"tool_dir" yaml:"tool_dir"`
BackupDir string `mapstructure:"backup_dir" yaml:"backup_dir"`

Quickstart struct {
DistFile string `mapstructure:"dist_file" yaml:"dist_file"`
LicenseFile string `mapstructure:"license_file" yaml:"license_file"`
} `mapstructure:"quickstart" yaml:"quickstart"`
OakRun struct {
DownloadURL string `mapstructure:"download_url" yaml:"download_url"`
StorePath string `mapstructure:"store_path" yaml:"store_path"`
} `mapstructure:"oak_run" yaml:"oak_run"`
} `mapstructure:"local" yaml:"local"`

Package struct {
Expand Down
19 changes: 17 additions & 2 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package common

const (
MainDir = "aem"
HomeDir = MainDir + "/home"
MainDir = "aem"
HomeDirName = "home"
HomeDir = MainDir + "/" + HomeDirName
VarDirName = "var"
VarDir = HomeDir + "/" + VarDirName
ConfigDirName = "etc"
ConfigDir = HomeDir + "/" + ConfigDirName
LogDirName = "log"
LogDir = VarDir + "/" + LogDirName
ToolDirName = "opt"
ToolDir = HomeDir + "/" + ToolDirName
LibDirName = "lib"
LibDir = HomeDir + "/" + LibDirName
TmpDirName = "tmp"
TmpDir = HomeDir + "/" + TmpDirName
DefaultDirName = "default"
DefaultDir = MainDir + "/" + DefaultDirName
)
38 changes: 38 additions & 0 deletions pkg/common/cryptox/cryptox.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cryptox

import (
"crypto/aes"
"crypto/sha256"
"encoding/hex"
"fmt"
log "github.com/sirupsen/logrus"
"io"
)

func EncryptString(key []byte, text string) string {
c, err := aes.NewCipher(key)
if err != nil {
log.Fatalf("encryption key/salt is invalid: %s", err)
}
encrypted := make([]byte, len(text))
c.Encrypt(encrypted, []byte(text))
return hex.EncodeToString(encrypted)
}

func DecryptString(key []byte, encrypted string) string {
c, err := aes.NewCipher(key)
if err != nil {
log.Fatalf("decryption key/salt is invalid: %s", err)
}
decoded, _ := hex.DecodeString(encrypted)
dest := make([]byte, len(decoded))
c.Decrypt(dest, decoded)
s := string(dest[:])
return s
}

func HashString(text string) string {
hash := sha256.New()
_, _ = io.WriteString(hash, text)
return fmt.Sprintf("%x", hash.Sum(nil))
}
14 changes: 7 additions & 7 deletions pkg/common/osx/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
)

type Lock[T comparable] struct {
path string
dataCurrent T
path string
dataProvider func() T
}

func NewLock[T comparable](path string, data T) Lock[T] {
return Lock[T]{path: path, dataCurrent: data}
func NewLock[T comparable](path string, dataProvider func() T) Lock[T] {
return Lock[T]{path, dataProvider}
}

func (l Lock[T]) Lock() error {
err := fmtx.MarshalToFile(l.path, l.dataCurrent)
err := fmtx.MarshalToFile(l.path, l.dataProvider())
if err != nil {
return fmt.Errorf("cannot save lock file '%s': %w", l.path, err)
}
Expand All @@ -36,7 +36,7 @@ func (l Lock[T]) IsLocked() bool {
}

func (l Lock[T]) DataCurrent() T {
return l.dataCurrent
return l.dataProvider()
}

func (l Lock[T]) DataLocked() (T, error) {
Expand All @@ -58,6 +58,6 @@ func (l Lock[T]) IsUpToDate() (bool, error) {
if err != nil {
return false, err
}
upToDate := cmp.Equal(l.dataCurrent, dataLocked)
upToDate := cmp.Equal(l.DataCurrent(), dataLocked)
return upToDate, nil
}
26 changes: 26 additions & 0 deletions pkg/common/tplx/tplx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tplx

import (
"bytes"
"fmt"
"github.com/wttech/aemc/pkg/common/filex"
"reflect"
"text/template"
)
Expand Down Expand Up @@ -33,3 +36,26 @@ var funcMap = template.FuncMap{
func recovery() {
recover()
}

func RenderString(tplContent string, data any) (string, error) {
tplParsed, err := New("string-template").Parse(tplContent)
if err != nil {
return "", err
}
var tplOutput bytes.Buffer
if err := tplParsed.Execute(&tplOutput, data); err != nil {
return "", err
}
return tplOutput.String(), nil
}

func RenderFile(file string, content string, data map[string]any) error {
scriptContent, err := RenderString(content, data)
if err != nil {
return err
}
if err := filex.WriteString(file, scriptContent); err != nil {
return fmt.Errorf("cannot render template file '%s': %w", file, err)
}
return nil
}
Loading

0 comments on commit 04506d6

Please sign in to comment.