Skip to content

Commit c47b661

Browse files
authored
various fixes (#9)
1 parent 8f9f046 commit c47b661

File tree

8 files changed

+41
-17
lines changed

8 files changed

+41
-17
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ spec:
4848
4949
### Step 1: Get an Argo CD token
5050
51-
The plugin requires a secret named `argocd-sync-token` with a key called `jwt.txt` containing the Argo CD token. See the [Argo CD documentation](https://argo-cd.readthedocs.io/en/stable/user-guide/projects/#project-roles) for information about generating tokens.
51+
The plugin requires a secret named `argocd-token` with a key called `token` containing the Argo CD token. See the [Argo CD documentation](https://argo-cd.readthedocs.io/en/stable/user-guide/projects/#project-roles) for information about generating tokens.
5252

5353
```yaml
5454
apiVersion: v1
5555
kind: Secret
5656
metadata:
57-
name: argocd-sync-token
57+
name: argocd-token
5858
stringData:
59-
jwt.txt: <token>
59+
token: <token>
6060
```
6161

6262
After defining the secret, apply it to your cluster:
6363

6464
```shell
65-
kubectl apply -f argocd-sync-token.yaml
65+
kubectl apply -f argocd-token.yaml
6666
```
6767

6868
### Step 2: Install the plugin

cmd/argocd-plugin/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,27 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"os"
67

78
"github.com/argoproj/argo-cd/v2/pkg/apiclient"
89

910
"github.com/crenshaw-dev/argocd-executor-plugin/internal"
1011
)
1112

1213
func main() {
14+
agentToken, err := os.ReadFile("/var/run/argo/token")
15+
if err != nil {
16+
panic(err.Error())
17+
}
18+
1319
client, err := apiclient.NewClient(&apiclient.ClientOptions{
1420
// TODO: make this configurable by passing a root CA.
1521
Insecure: true,
1622
})
1723
if err != nil {
1824
panic(fmt.Sprintf("failed to initialize Argo CD API client: %s", err))
1925
}
20-
executor := argocd.NewApiExecutor(client)
26+
executor := argocd.NewApiExecutor(client, string(agentToken))
2127
http.HandleFunc("/api/v1/template.execute", argocd.ArgocdPlugin(&executor))
2228
err = http.ListenAndServe(":3000", nil)
2329
if err != nil {

internal/argocd_executor.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"fmt"
88
"log"
9+
"net/http"
910
"strings"
1011
"sync"
1112
"time"
@@ -26,11 +27,20 @@ import (
2627
)
2728

2829
type ApiExecutor struct {
29-
apiClient apiclient.Client
30+
apiClient apiclient.Client
31+
agentToken string
3032
}
3133

32-
func NewApiExecutor(apiClient apiclient.Client) ApiExecutor {
33-
return ApiExecutor{apiClient: apiClient}
34+
func NewApiExecutor(apiClient apiclient.Client, agentToken string) ApiExecutor {
35+
return ApiExecutor{apiClient: apiClient, agentToken: agentToken}
36+
}
37+
38+
func (e *ApiExecutor) Authorize(req *http.Request) error {
39+
auth := req.Header.Get("Authorization")
40+
if auth != "Bearer "+e.agentToken {
41+
return fmt.Errorf("invalid agent token")
42+
}
43+
return nil
3444
}
3545

3646
func (e *ApiExecutor) Execute(args executor.ExecuteTemplateArgs) executor.ExecuteTemplateReply {
@@ -49,7 +59,12 @@ func (e *ApiExecutor) Execute(args executor.ExecuteTemplateArgs) executor.Execut
4959
return errorResponse(err)
5060
}
5161

52-
output, err := e.runAction(plugin.ArgoCD)
62+
if plugin.ArgoCD == nil {
63+
log.Println("unsupported plugin type")
64+
return executor.ExecuteTemplateReply{} // unsupported plugin
65+
}
66+
67+
output, err := e.runAction(*plugin.ArgoCD)
5368
if err != nil {
5469
return failedResponse(wfv1.Progress(fmt.Sprintf("0/1")), fmt.Errorf("action failed: %w", err))
5570
}
@@ -93,11 +108,13 @@ func (e *ApiExecutor) runAction(action ActionSpec) (out string, err error) {
93108
if action.App.Sync != nil {
94109
err = syncAppsParallel(*action.App.Sync, action.Timeout, appClient)
95110
if err != nil {
111+
return "", fmt.Errorf("failed to sync apps: %w", err)
96112
}
97113
}
98114
if action.App.Diff != nil {
99115
out, err = diffApp(*action.App.Diff, action.Timeout, appClient, settingsClient)
100116
if err != nil {
117+
return "", fmt.Errorf("failed to diff app: %w", err)
101118
}
102119
}
103120
return out, err

internal/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var (
2020

2121
// Executor performs the tasks requested by the Workflow.
2222
type Executor interface {
23+
Authorize(req *http.Request) error
2324
// Execute runs commands based on the args provided from the workflow
2425
Execute(args executor.ExecuteTemplateArgs) executor.ExecuteTemplateReply
2526
}

internal/types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package argocd
22

33
// PluginSpec represents the `plugin` block of an Argo Workflows template.
44
type PluginSpec struct {
5-
ArgoCD ActionSpec `json:"argocd,omitempty"`
5+
ArgoCD *ActionSpec `json:"argocd,omitempty"`
66
}
77

88
type ActionSpec struct {

manifests/argocd-executor-plugin-configmap.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ data:
77
- name: ARGOCD_AUTH_TOKEN
88
valueFrom:
99
secretKeyRef:
10-
key: jwt.txt
11-
name: argocd-sync-token
10+
key: token
11+
name: argocd-token
1212
- name: ARGOCD_SERVER
1313
value: argocd-server.argocd.svc.cluster.local
1414
image: crenshawdotdev/argocd-executor-plugin:v0.0.7

manifests/plugin.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ spec:
1717
- name: ARGOCD_AUTH_TOKEN
1818
valueFrom:
1919
secretKeyRef:
20-
name: argocd-sync-token
21-
key: jwt.txt
20+
name: argocd-token
21+
key: token
2222
- name: ARGOCD_SERVER
2323
value: argocd-server.argocd.svc.cluster.local
2424
ports:

scripts/setup_argocd.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ argocd proj role add-policy guestbook sync --action sync -o '*' --port-forward -
2929
argocd app create guestbook --upsert --project guestbook --repo https://github.com/argoproj/argocd-example-apps.git --path guestbook --dest-namespace guestbook --dest-server https://kubernetes.default.svc --directory-recurse --port-forward --port-forward-namespace argocd
3030

3131
# Create role token
32-
argocd proj role create-token guestbook sync -i argocd-workflows-plugin -t --port-forward --port-forward-namespace argocd | tr -d '\n' > ./jwt.txt
33-
kubectl create secret generic argocd-sync-token -n argo --save-config --dry-run=client --from-file=./jwt.txt -oyaml | kubectl apply -f -
34-
rm ./jwt.txt
32+
argocd proj role create-token guestbook sync -i argocd-workflows-plugin -t --port-forward --port-forward-namespace argocd | tr -d '\n' > ./token
33+
kubectl create secret generic argocd-token -n argo --save-config --dry-run=client --from-file=./token -oyaml | kubectl apply -f -
34+
rm ./token

0 commit comments

Comments
 (0)