Skip to content

Commit ee83ee1

Browse files
Removed ENV loading and added complete responsibility of user configs to yaml file (even name of env variables in case of secrets)
1 parent 940e82a commit ee83ee1

File tree

8 files changed

+115
-37
lines changed

8 files changed

+115
-37
lines changed

cmd/laclm/main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,8 @@ func exec() error {
7070
os.Exit(1)
7171
}
7272

73-
/*
74-
load environment variables
75-
if there is an error or environment variables are not set, then it will exit with code 1
76-
*/
77-
config.LoadEnv()
78-
7973
fmt.Println("loaded config")
80-
74+
8175
/*
8276
true for production, false for development mode
8377
logger is only for http server and core components (after this step)

config.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
app:
55
name: laclm-dev
66
version: v1.1
7-
debug_mode: true
7+
debug_mode: false
88

99
# backend server deployment configs
1010
server:
@@ -15,7 +15,7 @@ server:
1515
database:
1616
transaction_logs_redis:
1717
address: localhost
18-
password: testingpassword
18+
password: ${LACLM_TRANS_REDIS_PASSWORD}
1919
db: 1
2020

2121
# logging configurations
@@ -37,6 +37,11 @@ filesystem_servers:
3737
# authentication information
3838
authentication:
3939
ldap:
40+
tls: true
41+
address: "ldaps://ldap.example.com"
42+
admin_dn: ${LACLM_LDAP_ADMIN_DN}
43+
admin_password: ${LACLM_LDAP_ADMIN_PASSWORD}
4044

4145
backend_security:
46+
jwt_secret_key: ${JWT_SECRET_KEY}
4247
jwt_expiry: 1

config/authentication.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package config
2+
3+
import (
4+
"errors"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
)
8+
9+
/* authentication parameters */
10+
type Authentication struct {
11+
LDAPConfig LDAPConfig `yaml:"ldap"`
12+
}
13+
14+
/* ldap authentication parameters */
15+
type LDAPConfig struct {
16+
TLS bool `yaml:"tls"`
17+
Address string `yaml:"address"`
18+
AdminDN string `yaml:"admin_dn"`
19+
AdminPassword string `yaml:"admin_password"`
20+
}
21+
22+
/* normalization function */
23+
func (a *Authentication) Normalize() error {
24+
return a.LDAPConfig.Normalize()
25+
}
26+
27+
func (l *LDAPConfig) Normalize() error {
28+
/* TLS will be false by default */
29+
30+
if l.Address == "" {
31+
return errors.New(heredoc.Doc(`
32+
LDAP address is not specified in the configuration file.
33+
34+
Please check the docs for more information:
35+
`))
36+
}
37+
38+
if l.AdminDN == "" {
39+
return errors.New(heredoc.Doc(`
40+
LDAP admin DN is not specified in the configuration file.
41+
42+
Please check the docs for more information:
43+
`))
44+
}
45+
46+
if l.AdminPassword == "" {
47+
return errors.New(heredoc.Doc(`
48+
LDAP admin password is not specified in the configuration file.
49+
50+
Please check the docs for more information:
51+
`))
52+
}
53+
54+
return nil
55+
}

config/backend_security.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
package config
22

3+
import (
4+
"errors"
5+
6+
"github.com/MakeNowJust/heredoc"
7+
)
8+
39
/* backend security configs */
410
type BackendSecurity struct {
5-
JWTExpiry int `yaml:"jwt_expiry"`
11+
JWTTokenSecret string `yaml:"jwt_secret_token"`
12+
JWTExpiry int `yaml:"jwt_expiry"`
613
}
714

815
/* normalization function */
916
func (b *BackendSecurity) Normalize() error {
17+
if b.JWTTokenSecret == "" {
18+
return errors.New(heredoc.Doc(`
19+
Transaction Log Redis Address is not specified in the configuration file.
20+
21+
Please check the docs for more information:
22+
`))
23+
}
24+
1025
if b.JWTExpiry == 0 {
1126
b.JWTExpiry = 1
1227
}

config/config.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import "fmt"
55
/* globally accessible yaml config */
66
var BackendConfig Config
77

8-
/* globally accessible environment variables */
9-
var EnvConfig EnvironmentConfig
10-
118
/* complete yaml config for global usage */
129
type Config struct {
1310
AppInfo App `yaml:"app"`
@@ -16,11 +13,7 @@ type Config struct {
1613
Logging Logging `yaml:"logging"`
1714
FileSystemServers []FileSystemServers `yaml:"filesystem_servers"`
1815
BackendSecurity BackendSecurity `yaml:"backend_security"`
19-
}
20-
21-
/* complete environment variables configs for global usage */
22-
type EnvironmentConfig struct {
23-
JWTSecret string
16+
Authentication Authentication `yaml:"authentication"`
2417
}
2518

2619
/* complete config normalizer function */
@@ -51,5 +44,9 @@ func (c *Config) Normalize() error {
5144
return fmt.Errorf("backend security configuration error: %w", err)
5245
}
5346

47+
if err := c.Authentication.Normalize(); err != nil {
48+
return fmt.Errorf("authentication configuration error: %w", err)
49+
}
50+
5451
return nil
5552
}

config/loader.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"os"
66

7-
"go.uber.org/zap"
87
"gopkg.in/yaml.v3"
98
"github.com/davecgh/go-spew/spew"
109
)
@@ -27,14 +26,18 @@ func LoadConfig(path string) error {
2726

2827
}
2928

29+
/* expand all environment variables in the yaml config */
30+
expanded := os.ExpandEnv(string(data))
31+
3032
/* unmarshal the yaml file to defined struct */
31-
err = yaml.Unmarshal(data, &BackendConfig)
33+
err = yaml.Unmarshal([]byte(expanded), &BackendConfig)
3234
if err != nil {
3335
return fmt.Errorf("config loading error %w",
3436
err,
3537
)
3638
}
3739

40+
/* write the config file in console if in debug mode */
3841
if BackendConfig.AppInfo.DebugMode {
3942
fmt.Println("Contents of Config File (debug mode ON)")
4043
spew.Dump(BackendConfig)
@@ -44,15 +47,3 @@ func LoadConfig(path string) error {
4447
/* normalize the complete backend config before proceeding */
4548
return BackendConfig.Normalize()
4649
}
47-
48-
/* loads environment variables */
49-
func LoadEnv() {
50-
51-
/* get the JWT_SECRET_KEY from environment variable */
52-
secret := os.Getenv("JWT_SECRET_KEY")
53-
if secret == "" {
54-
zap.L().Fatal("JWT_SECRET_KEY environment variable not set")
55-
}
56-
57-
EnvConfig.JWTSecret = secret
58-
}

internal/authentication/authentication.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func GenerateJWT(username string) (string, error) {
2424
"exp": time.Now().Add(time.Hour * time.Duration(expiryHours)).Unix(),
2525
})
2626

27-
return token.SignedString([]byte(config.EnvConfig.JWTSecret))
27+
return token.SignedString([]byte(config.BackendConfig.BackendSecurity.JWTTokenSecret))
2828
}
2929

3030
/* validate JWT token and return claims */
@@ -33,7 +33,7 @@ func ValidateJWT(tokenString string) (jwt.MapClaims, error) {
3333
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
3434
return nil, fmt.Errorf("unexpected signing method")
3535
}
36-
return config.EnvConfig.JWTSecret, nil
36+
return config.BackendConfig.BackendSecurity.JWTTokenSecret, nil
3737
})
3838

3939
if err != nil {

internal/ldap/ldap.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package ldap
22

33
import (
4+
"os"
5+
"crypto/tls"
46
"fmt"
5-
"go.uber.org/zap"
67

8+
"github.com/PythonHacker24/linux-acl-management-backend/config"
79
"github.com/go-ldap/ldap/v3"
10+
"go.uber.org/zap"
811
)
912

1013
/* authenticate a user with ldap */
@@ -18,8 +21,22 @@ func AuthenticateUser(username, password, searchbase string) bool {
1821
reducing unauthorized access in edge cases
1922
*/
2023

24+
var l *ldap.Conn
25+
var err error
26+
ldapAddress := config.BackendConfig.Authentication.LDAPConfig.Address
27+
28+
if config.BackendConfig.Authentication.LDAPConfig.TLS {
29+
l, err = ldap.DialURL(ldapAddress, ldap.DialWithTLSConfig(&tls.Config{
30+
31+
/* true if using self-signed certs (not recommended) */
32+
InsecureSkipVerify: false,
33+
}))
34+
} else {
35+
l, err = ldap.DialURL(ldapAddress)
36+
}
37+
2138
/* dial to the ldap server */
22-
l, err := ldap.DialURL("")
39+
l, err = ldap.DialURL("")
2340
if err != nil {
2441
zap.L().Error("Failed to connect to LDAP Server",
2542
zap.Error(err),
@@ -28,8 +45,12 @@ func AuthenticateUser(username, password, searchbase string) bool {
2845
}
2946
defer l.Close()
3047

48+
/* securely fetch LDAP credentials from the environment */
49+
adminDN := os.Getenv("LDAP_ADMIN_DN")
50+
adminPassword := os.Getenv("LDAP_ADMIN_PASSWORD")
51+
3152
/* authenticating with the ldap server with admin */
32-
err = l.Bind("", "")
53+
err = l.Bind(adminDN, adminPassword)
3354
if err != nil {
3455
zap.L().Error("Admin authentication failed",
3556
zap.Error(err),

0 commit comments

Comments
 (0)