Skip to content

Commit

Permalink
fix: make plugin a little cleaner
Browse files Browse the repository at this point in the history
but still no tests
  • Loading branch information
jakobmoellerdev committed Dec 5, 2024
1 parent 978d9df commit 5d4199c
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 257 deletions.
7 changes: 5 additions & 2 deletions api/ocm/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,14 @@ func (p *pluginImpl) Exec(r io.Reader, w io.Writer, args ...string) (result []by
// Not totally safe, but better than nothing.
logargs := make([]string, len(args))
for i, arg := range args {
if logargs[i] != "" {
continue
}
if strings.Contains(arg, "credentials") {
if strings.Contains(arg, "=") {
arg = "***"
logargs[i] = "***"
} else if i < len(args)-1 {
args[i+1] = "***"
logargs[i+1] = "***"
}
}
logargs[i] = arg
Expand Down
53 changes: 47 additions & 6 deletions cmds/jfrogplugin/main.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
package main

import (
"encoding/json"
"fmt"
"os"
"strconv"

"ocm.software/ocm/api/config"
"ocm.software/ocm/api/ocm/extensions/artifacttypes"
"ocm.software/ocm/api/ocm/extensions/blobhandler"
"ocm.software/ocm/api/ocm/plugin"
"ocm.software/ocm/api/ocm/plugin/ppi"
"ocm.software/ocm/api/ocm/plugin/ppi/cmds"
"ocm.software/ocm/api/version"
"ocm.software/ocm/cmds/jfrogplugin/uploaders"
"ocm.software/ocm/cmds/jfrogplugin/uploaders/helm"
)

const NAME = "jfrog"

func main() {
p := ppi.NewPlugin("jfrog", version.Get().String())
p := ppi.NewPlugin(NAME, version.Get().String())

p.SetShort(NAME + " plugin")
p.SetLong(`ALPHA GRADE plugin providing custom functions related to interacting with JFrog Repositories (e.g. Artifactory).
This plugin is solely for interacting with JFrog Servers and should not be used for generic repository interactions.
Thus, you should only consider this plugin if
- You need to use a JFrog specific API
- You cannot use any of the generic (non-jfrog) implementations.
p.SetShort("JFrog plugin")
p.SetLong("plugin providing custom functions related to interacting with JFrog Repositories (e.g. Artifactory).")
p.SetConfigParser(uploaders.GetConfig)
Examples:
u := uploaders.New()
You can configure the JFrog plugin as an Uploader in an ocm config file with:
- type: ` + fmt.Sprintf("%s.ocm.%s", plugin.KIND_UPLOADER, config.OCM_CONFIG_TYPE_SUFFIX) + `
registrations:
- name: ` + fmt.Sprintf("%s/%s/%s", plugin.KIND_PLUGIN, NAME, helm.NAME) + `
artifactType: ` + artifacttypes.HELM_CHART + `
priority: 200 # must be > ` + strconv.Itoa(blobhandler.DEFAULT_BLOBHANDLER_PRIO) + ` to be used over the default handler
config:
type: ` + fmt.Sprintf("%s/%s", helm.NAME, helm.VERSION) + `
# this is only a sample JFrog Server URL, do NOT append /artifactory
url: int.repositories.ocm.software
repository: ocm-helm-test
`)
p.SetConfigParser(GetConfig)

u := helm.New()
if err := p.RegisterUploader(artifacttypes.HELM_CHART, "", u); err != nil {
panic(err)
}
Expand All @@ -26,3 +55,15 @@ func main() {
os.Exit(1)
}
}

type Config struct {
}

func GetConfig(raw json.RawMessage) (interface{}, error) {
var cfg Config

if err := json.Unmarshal(raw, &cfg); err != nil {
return nil, fmt.Errorf("could not get config: %w", err)
}
return &cfg, nil
}
249 changes: 0 additions & 249 deletions cmds/jfrogplugin/uploaders/helm.go

This file was deleted.

25 changes: 25 additions & 0 deletions cmds/jfrogplugin/uploaders/helm/headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package helm

import (
"net/http"

"ocm.software/ocm/api/credentials"
)

func SetHeadersFromCredentials(req *http.Request, creds credentials.Credentials) {
if creds == nil {
return
}
if creds.ExistsProperty(credentials.ATTR_TOKEN) {
req.Header.Set("Authorization", "Bearer "+creds.GetProperty(credentials.ATTR_TOKEN))
} else {
var user, pass string
if creds.ExistsProperty(credentials.ATTR_USERNAME) {
user = creds.GetProperty(credentials.ATTR_USERNAME)
}
if creds.ExistsProperty(credentials.ATTR_PASSWORD) {
pass = creds.GetProperty(credentials.ATTR_PASSWORD)
}
req.SetBasicAuth(user, pass)
}
}
Loading

0 comments on commit 5d4199c

Please sign in to comment.