Skip to content

Commit

Permalink
Merge pull request #4 from kha7iq/set-custom-config-location
Browse files Browse the repository at this point in the history
feat: set custom config location
  • Loading branch information
kha7iq authored Nov 23, 2023
2 parents 706a1c4 + 4d06b15 commit 7e2d7af
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 42 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ name: build
on:
push:
tags:
- 'v*'
- 'v0.[0-9]+.[0-9]'

jobs:
build:
Build-and-Release:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: '>=1.20.0'
go-version: '>=1.20.5'


- name: Release
uses: goreleaser/goreleaser-action@v3
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ sudo rpm -i kc-ssh-pam_amd64.rpm

```bash
# Chose desired version
export KC_SSH_PAM_VERSION="0.1.1"
export KC_SSH_PAM_VERSION="0.1.2"
wget -q https://github.com/kha7iq/kc-ssh-pam/releases/download/v${KC_SSH_PAM_VERSION}/kc-ssh-pam_linux_amd64.tar.gz && \
tar -xf kc-ssh-pam_linux_amd64.tar.gz && \
chmod +x kc-ssh-pam && \
Expand All @@ -69,15 +69,16 @@ Generates a password grant token from Keycloak for the given user.
Options:
-h, --help Show this help message and exit
-v, --version Show version information
-c Set configuration file path

Notes:
For the program to function properly, it needs to locate a configuration file called 'config.toml'.
The program will search for this file in the current directory, default install '/opt/kc-ssh-pam', '/etc/config.toml',
and '$HOME/.config/config.toml', in that specific order.
The program will search for this file in the current directory, '/opt/kc-ssh-pam' and '$HOME/.config/config.toml',
in that specific order. You can also set a custom path by specifying KC_SSH_CONFIG variable or -c flag which takes prefrence.

In addition to defaults, all configuration parameters can also be provided through environment variables.

$KC_SSH_REALM $KC_SSH_ENDPOINT $KC_SSH_CLIENTID $KC_SSH_CLIENTSECRET $KC_SSH_CLIENTSCOPE
KC_SSH_CONFIG KC_SSH_REALM KC_SSH_ENDPOINT KC_SSH_CLIENTID KC_SSH_CLIENTSECRET KC_SSH_CLIENTSCOPE

To use the program, you must create a client in Keycloak and provide the following
information in the configuration file: realm, endpoint, client ID, client secret, and
Expand All @@ -89,16 +90,18 @@ Arguments:
OTP (Optional) The OTP code if two-factor authentication is enabled i.e (password/otp)

EXAMPLE (With otp): echo testpass/717912 | kc-ssh-pam (Only Password): echo testpass | kc-ssh-pam

```
## Configuration
For the program to function properly, it needs to locate a configuration file called `config.toml`.
The program will search for this file in the follwoing order..
1. Present working directory
2. Default install location `/opt/kc-ssh-pam/config.toml`
3. System `/etc/config.toml`,
4. `$HOME/.config/config.toml`
1. If a configuration path is specified using the `-c`` flag, it will override any other defined options.
2. Verify the existence of the KC_SSH_CONFIG variable; if it's defined, use the config location specified within it.
3. The working directory where the program is being executed from.
4. Default install location `/opt/kc-ssh-pam/config.toml`
5. `$HOME/.config/config.toml`
`config.toml`
Expand Down
4 changes: 2 additions & 2 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func VerifyToken(aToken, cID, cSecret, providerRealm, providerUrl string) error
// Parse the access token
token, _, err := parser.ParseUnverified(aToken, jwt.MapClaims{})
if err != nil {
return fmt.Errorf("Error parsing access token:", err)
return fmt.Errorf("Error parsing access token: %v", err)

}

Expand All @@ -57,7 +57,7 @@ func VerifyToken(aToken, cID, cSecret, providerRealm, providerUrl string) error
// Verify the access token with Keycloak
_, err = client.RetrospectToken(context.Background(), aToken, cID, cSecret, providerRealm)
if err != nil {
return fmt.Errorf("Access token verification failed:", err)
return fmt.Errorf("Access token verification failed: %v", err)
}

return nil
Expand Down
34 changes: 15 additions & 19 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package conf

import "github.com/spf13/viper"
import (
"os"

"github.com/spf13/viper"
)

// Config struct will store the configuration values provided by user
type Config struct {
Realm string
Endpoint string
Expand All @@ -17,32 +20,25 @@ var (
"endpoint": "localhost",
"scope": "openid",
}
configName = "config"
configPaths = []string{
".",
"/opt/kc-ssh-pam",
"/etc/",
"$HOME/.config/",
}
ConfigPath string
)

func LoadConfig() (config Config, err error) {
for k, v := range defaults {
viper.SetDefault(k, v)
}
for _, p := range configPaths {
viper.AddConfigPath(p)
if len(ConfigPath) == 0 {

ConfigPath = os.Getenv("KC_SSH_CONFIG")
}

viper.SetConfigName(configName)
viper.SetConfigFile(ConfigPath)
viper.SetConfigType("toml")

viper.SetEnvPrefix("kc_ssh") // Becomes "KC_SSH"
viper.BindEnv("Realm") // KC_SSH_REALM
viper.BindEnv("Endpoint") // KC_SSH_ENDPOINT
viper.BindEnv("ClientID") // KC_SSH_CLIENTID
viper.BindEnv("ClientSecret") // KC_SSH_CLIENTSECRET
viper.BindEnv("ClientScope") // KC_SSH_CLIENTSCOPE
viper.AddConfigPath("/opt/kc-ssh-pam")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.config")
viper.SetEnvPrefix("kc_ssh")
viper.AutomaticEnv()

err = viper.ReadInConfig()
if err != nil {
Expand Down
19 changes: 14 additions & 5 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"fmt"
"os"

"github.com/kha7iq/kc-ssh-pam/internal/conf"
)

func printHelpMessage() {
Expand All @@ -14,15 +16,16 @@ Generates a password grant token from Keycloak for the given user.
Options:
-h, --help Show this help message and exit
-v, --version Show version information
-c Set configuration file path
Notes:
For the program to function properly, it needs to locate a configuration file called 'config.toml'.
The program will search for this file in the current directory, default install '/opt/kc-ssh-pam', '/etc/config.toml',
and '$HOME/.config/config.toml', in that specific order.
The program will search for this file in the current directory, '/opt/kc-ssh-pam' and '$HOME/.config/config.toml',
in that specific order. You can also set a custom path by specifying KC_SSH_CONFIG variable or -c flag which takes prefrence.
In addition to defaults, all configuration parameters can also be provided through environment variables.
$KC_SSH_REALM $KC_SSH_ENDPOINT $KC_SSH_CLIENTID $KC_SSH_CLIENTSECRET $KC_SSH_CLIENTSCOPE
KC_SSH_CONFIG KC_SSH_REALM KC_SSH_ENDPOINT KC_SSH_CLIENTID KC_SSH_CLIENTSECRET KC_SSH_CLIENTSCOPE
To use the program, you must create a client in Keycloak and provide the following
information in the configuration file: realm, endpoint, client ID, client secret, and
Expand All @@ -37,15 +40,20 @@ Arguments:
`, os.Args[0])
}

// displayVersion displays build version information
func DisplayHelp(version, buildDate, commitSha string) {
// ParseFlags function will parse the flags from command line.
func ParseFlags(version, buildDate, commitSha string) {
var configPathFlag string
helpFlag := flag.Bool("help", false, "Show this help message and exit")
hFlag := flag.Bool("h", false, "Show this help message and exit")
versionFlag := flag.Bool("version", false, "Display version information")
vFlag := flag.Bool("v", false, "Display version number (shorthand)")
flag.StringVar(&configPathFlag, "c", "", "Set configuration file path")

flag.Parse()

// Set conf.ConfigPath after parsing flags
conf.ConfigPath = configPathFlag

if *helpFlag || *hFlag {
printHelpMessage()
os.Exit(0)
Expand All @@ -59,6 +67,7 @@ func DisplayHelp(version, buildDate, commitSha string) {
}
}

// printVersionInfo displays build version information
func printVersionInfo(version, buildDate, commitSha string) {
fmt.Println("Version:", version)
fmt.Println("Build Date:", buildDate)
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ var (
)

func main() {
// displayVersion()
flags.DisplayHelp(version, buildDate, commitSha)
flags.ParseFlags(version, buildDate, commitSha)
c, err := conf.LoadConfig()
if err != nil {
log.Fatalf("Error reading config file: %s", err)
Expand Down

0 comments on commit 7e2d7af

Please sign in to comment.