Skip to content

Commit ea1a9fc

Browse files
authoredSep 20, 2017
Merge pull request #30 from indigo-dc/oidc-agent
Oidc agent, fix #29
2 parents d9503a3 + 2f6b0df commit ea1a9fc

File tree

2 files changed

+104
-20
lines changed

2 files changed

+104
-20
lines changed
 

‎gitbook/user.md

+36-13
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ Using orchent is made as easy as possible. In case you are lost orchent provides
33
information with its 'help' command, just call `orchent --help`.
44

55
## Setting The Access Token
6-
The orchestrator needs a way to authorize orchent, this is done by a so called access token.
7-
The access token is retrieved beforhand at either [IAM](https://github.com/indigo-iam/iam) or
8-
[WaTTS](https://github.com/indigo-dc/tts).
6+
Orchent uses so called access token to authorize itself against the orchestrator.
97

10-
Once an access token is known, it needs to be exportet in the environment variable
8+
In The newest release orchent supports the usage of the [oidc-agent](https://github.com/indigo-dc/oidc-agent). By using the oidc-agent the need to copy and paste access tokens is history.
9+
Two things need to be done to use the oidc-agent with orcht. The first thing is to export the
10+
name of the oidc-agent account to use in the environmental variable 'ORCHENT_AGENT_ACCOUNT'.
11+
The account must be loaded into the agent before usage. The second thing is to ensure that
12+
the path to the socket of the oidc-agent is set within the variable 'OIDC_SOCK':
13+
14+
```
15+
export ORCHENT_AGENT_ACCOUNT=<account name>
16+
export OIDC_SOCK=<path to socket of oidc-agent>
17+
```
18+
19+
One can still set the access token directly in the environmental variable 'ORCHENT_TOKEN',
20+
this overrides the previous settings.
1121
`ORCHENT_TOKEN`:
1222
```
1323
export ORCHENT_TOKEN=<your access token here>
@@ -60,16 +70,29 @@ orchent depshow one
6070
Please make sure you have exported your access token, see above.
6171

6272
### Getting help
63-
orchent provides a lot of help, the main help is shown by running `orchent help`.
73+
orchent provides a lot of help, the main help is shown by running `orchent --help`.
6474
The output is:
6575
```
66-
$ orchent help
67-
usage: orchent --url=URL [<flags>] <command> [<args> ...]
76+
$ orchent --help
77+
usage: orchent [<flags>] <command> [<args> ...]
78+
79+
The orchestrator client.
80+
81+
82+
Please either store your access token in 'ORCHENT_TOKEN' or set the account to use with oidc-agent
83+
in the 'ORCHENT_AGENT_ACCOUNT' and the socket of the oidc-agent in the 'OIDC_SOCK' environment
84+
variable:
85+
86+
export ORCHENT_TOKEN=<your access token>
87+
OR
88+
export OIDC_SOCK=<path to the oidc-agent socket> (usually this is already exported)
89+
export ORCHENT_AGENT_ACCOUNT=,account to use>
90+
91+
If you need to specify the file containing the trusted root CAs use the 'ORCHENT_CAFILE' environment
92+
variable:
93+
94+
export ORCHENT_CAFILE=<path to file containing trusted CAs>
6895
69-
The orchestrator client. Please store your access token in the 'ORCHENT_TOKEN' environment
70-
variable: 'export ORCHENT_TOKEN=<your access token>'. If you need to specify the file
71-
containing the trusted root CAs use the 'ORCHENT_CAFILE' environment variable:
72-
'export ORCHENT_CAFILE=<path to file containing trusted CAs>'.
7396
7497
Flags:
7598
--help Show context-sensitive help (also try --help-long and --help-man).
@@ -106,8 +129,8 @@ Commands:
106129
show a specific resource of a given deployment
107130
108131
test
109-
test if the given url is pointing to an orchestrator, please use this to ensure
110-
there is no typo in the url.
132+
test if the given url is pointing to an orchestrator, please use this to ensure there is no
133+
typo in the url.
111134
112135
```
113136

‎orchent.go

+68-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/dghubble/sling"
1010
"github.com/zpatrick/go-config"
1111
"gopkg.in/alecthomas/kingpin.v2"
12+
"net"
1213
"net/http"
1314
"net/url"
1415
"os"
@@ -17,10 +18,10 @@ import (
1718
"strings"
1819
)
1920

20-
const OrchentVersion string = "1.1.0"
21+
const OrchentVersion string = "1.2.0"
2122

2223
var (
23-
app = kingpin.New("orchent", "The orchestrator client. Please store your access token in the 'ORCHENT_TOKEN' environment variable: 'export ORCHENT_TOKEN=<your access token>'. If you need to specify the file containing the trusted root CAs use the 'ORCHENT_CAFILE' environment variable: 'export ORCHENT_CAFILE=<path to file containing trusted CAs>'.").Version(OrchentVersion)
24+
app = kingpin.New("orchent", "The orchestrator client. \n \nPlease either store your access token in 'ORCHENT_TOKEN' or set the account to use with oidc-agent in the 'ORCHENT_AGENT_ACCOUNT' and the socket of the oidc-agent in the 'OIDC_SOCK' environment variable: \n export ORCHENT_TOKEN=<your access token> \n OR \n export OIDC_SOCK=<path to the oidc-agent socket> (usually this is already exported) \n export ORCHENT_AGENT_ACCOUNT=,account to use> \nIf you need to specify the file containing the trusted root CAs use the 'ORCHENT_CAFILE' environment variable: \n export ORCHENT_CAFILE=<path to file containing trusted CAs>\n \n").Version(OrchentVersion)
2425
hostUrl = app.Flag("url", "the base url of the orchestrator rest interface. Alternative the environment variable 'ORCHENT_URL' can be used: 'export ORCHENT_URL=<the_url>'").Short('u').String()
2526

2627
lsDep = app.Command("depls", "list deployments")
@@ -577,19 +578,79 @@ func try_alias_uuid(alias string, aliases map[string]string) string {
577578
return alias
578579
}
579580

581+
func get_account() (issuerSet bool, agentIssuer string) {
582+
agentAccount, accountSet := os.LookupEnv("ORCHENT_AGENT_ACCOUNT")
583+
// issuerValue, issuerSet = os.LookupEnv("ORCHENT_ISSUER")
584+
// if !agentSet && issuerSet {
585+
// agentIssuer = issuerValue
586+
// }
587+
return accountSet, agentAccount
588+
}
589+
590+
func user_info(format string, a ...interface{}) {
591+
fmt.Printf(format, a)
592+
}
593+
594+
func try_agent_token(account string) (tokenSet bool, tokenValue string) {
595+
socketValue, socketSet := os.LookupEnv("OIDC_SOCK")
596+
tokenSet = false
597+
tokenValue = ""
598+
if !socketSet {
599+
return tokenSet, tokenValue
600+
}
601+
602+
c, err := net.Dial("unixpacket", socketValue)
603+
if err != nil {
604+
user_info("could not connect to socket %s: %s\n", socketValue, err.Error())
605+
return tokenSet, tokenValue
606+
}
607+
defer c.Close()
608+
609+
ipcReq := fmt.Sprintf(`{"request":"access_token","account":"%s","min_valid_period":120}`, account)
610+
_, err = c.Write([]byte(ipcReq))
611+
if err != nil {
612+
user_info("could not write to socket %s: %s\n", socketValue, err.Error())
613+
return tokenSet, tokenValue
614+
}
615+
var response = [4096]byte{}
616+
length, err := c.Read(response[0:4095])
617+
if err != nil {
618+
user_info("could not read from socket %s: %s\n", socketValue, err.Error())
619+
return tokenSet, tokenValue
620+
}
621+
622+
response[length] = 0
623+
oidcToken := make(map[string]string)
624+
jsonErr := json.Unmarshal(response[0:length], &oidcToken)
625+
if jsonErr != nil {
626+
user_info("error parsing the oidc response: %s\n", jsonErr)
627+
return tokenSet, tokenValue
628+
}
629+
tokenValue, tokenSet = oidcToken["access_token"]
630+
if tokenSet {
631+
user_info("received token from oidc-agent\n")
632+
}
633+
return tokenSet, tokenValue
634+
}
635+
636+
func try_token(accountSet bool, account string) (tokenSet bool, token string) {
637+
tokenValue, tokenSet := os.LookupEnv("ORCHENT_TOKEN")
638+
if !tokenSet && accountSet {
639+
return try_agent_token(account)
640+
}
641+
return tokenSet, tokenValue
642+
}
643+
580644
func base_connection(urlBase string) *sling.Sling {
581645
client := client()
582-
tokenValue, tokenSet := os.LookupEnv("ORCHENT_TOKEN")
583-
genTokenValue, genTokenSet := os.LookupEnv("OIDC_AT")
646+
accountSet, account := get_account()
647+
tokenSet, tokenValue := try_token(accountSet, account)
584648
base := sling.New().Client(client).Base(urlBase)
585649
base = base.Set("User-Agent", "Orchent")
586650
base = base.Set("Accept", "application/json")
587651
if tokenSet {
588652
token := "Bearer " + tokenValue
589653
return base.Set("Authorization", token)
590-
} else if genTokenSet {
591-
token := "Bearer " + genTokenValue
592-
return base.Set("Authorization", token)
593654
} else {
594655
fmt.Println(" ")
595656
fmt.Println("*** WARNING: no access token has been specified ***")

0 commit comments

Comments
 (0)
Please sign in to comment.