Skip to content

Commit

Permalink
update logging
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Jul 12, 2019
1 parent c9eeb8c commit 388574e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 14 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ git push origin --tags
---

TODO
* [ ] add logging for every error e.g. `return reconcile.Result{}, err`
* [ ] review logging of sensible informations
* [ ] fix version of lastpass-cli and alpine
* [ ] add license
* [ ] publish to [OperatorHub](https://operatorhub.io/contribute)
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/lastpass/lastpass_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,24 @@ func (r *ReconcileLastPass) Reconcile(request reconcile.Request) (reconcile.Resu

// Set LastPassSecret instance as the owner and controller
if err := controllerutil.SetControllerReference(instance, desired, r.scheme); err != nil {
reqLogger.Error(err, "Failed to set LastPassSecret instance as the owner and controller")
return reconcile.Result{}, err
}

// Check if this Secret already exists
current := &corev1.Secret{}
err = r.client.Get(context.TODO(), types.NamespacedName{Name: desired.Name, Namespace: desired.Namespace}, current)
if err != nil && errors.IsNotFound(err) {
reqLogger.Info("Creating a new Secret", "Secret.Namespace", desired.Namespace, "Secret.Name", desired.Name)
reqLogger.Info("Creating Secret", "Secret.Namespace", desired.Namespace, "Secret.Name", desired.Name)
err = r.client.Create(context.TODO(), desired)
if err != nil {
reqLogger.Error(err, "Failed to create Secret", "Secret.Namespace", desired.Namespace, "Secret.Name", desired.Name)
return reconcile.Result{}, err
}
// Secret created successfully - don't requeue
continue
} else if err != nil {
reqLogger.Error(err, "Failed to get Secret", "Secret.Namespace", desired.Namespace, "Secret.Name", desired.Name)
return reconcile.Result{}, err
}

Expand All @@ -155,6 +158,7 @@ func (r *ReconcileLastPass) Reconcile(request reconcile.Request) (reconcile.Resu
"Desired:LastTouch", desired.Annotations["lastTouch"])
err = r.client.Update(context.TODO(), desired)
if err != nil {
reqLogger.Error(err, "Failed to update Secret", "Secret.Namespace", desired.Namespace, "Secret.Name", desired.Name)
return reconcile.Result{}, err
}
// Secret updated successfully - don't requeue
Expand Down
14 changes: 7 additions & 7 deletions pkg/lastpass/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ type LastPassSecret struct {
func VerifyCliExistsOrDie() {
out, err := sh.Command("which", "lpass").Output()
if err != nil || "" == string(out) {
panic("lpass binary not found")
panic(fmt.Sprintf("lpass binary not found: [%s]", err))
}
log.Printf("lpass binary found")
}

// Login attempts to login using lastpass-cli
// Login using lastpass-cli
func Login(username string, password string) error {
// echo <PASSWORD> | LPASS_DISABLE_PINENTRY=1 lpass login --trust <USERNAME>
out, err := sh.NewSession().SetEnv("LPASS_DISABLE_PINENTRY", "1").Command("echo", password).Command("lpass", "login", "--trust", username).Output()
if err != nil || "" == string(out) {
// sometimes returns error: "Error: HTTP response code said error" even if the credentials are valid
return fmt.Errorf("unable to login: verify credentials - %s", err)
return fmt.Errorf("verify credentials, unable to login: %s", err)
}
log.Printf("Succesfully logged in")
return nil
Expand All @@ -62,16 +62,16 @@ func RequestSecrets(group string, name string) ([]LastPassSecret, error) {
fullName := buildFullName(group, name)
secrets := []LastPassSecret{}

log.Printf("Request secret: [group=%s][name=%s][fullName=%s]", group, name, fullName)
log.Printf("Request secrets: [%s]", fullName)

// lpass show <GROUP>/<NAME> --json --expand-multi
out, err := sh.Command("lpass", "show", fullName, "--json", "--expand-multi").Output()
if err != nil {
return secrets, fmt.Errorf("invalid secret: [%s] - %s", fullName, err)
return secrets, fmt.Errorf("invalid secrets: [%s] - %s", fullName, err)
}

// TODO print in debug only
log.Printf("Secret response: %s", out)
// uncomment for debug
//log.Printf("Secret response: %s", out)

// decode JSON structure into Go structure
jsonErr := json.Unmarshal([]byte(out), &secrets)
Expand Down
7 changes: 3 additions & 4 deletions pkg/utils/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"os"
)

// TODO remove value from print or change debug level
// GetEnvOrDie retrieve an environment variable or exit
func GetEnvOrDie(key string) string {
if value, ok := os.LookupEnv(key); ok {
log.Printf("Environment variable found: [%s=%s]", key, value)
log.Printf("Found environment variable: [%s]", key)
return value
} else {
panic(fmt.Sprintf("No environment variable found: [%s]", key))
}
panic(fmt.Sprintf("No environment variable found: [%s]", key))
}

0 comments on commit 388574e

Please sign in to comment.