-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add source code for "Secure Your Services" chapter - part 2 (#35)
- Loading branch information
1 parent
17ebde4
commit 9fae48c
Showing
15 changed files
with
668 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/casbin/casbin" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func New(model, policy string) *Authorizer { | ||
enforcer := casbin.NewEnforcer(model, policy) | ||
return &Authorizer{ | ||
enforcer: enforcer, | ||
} | ||
} | ||
|
||
type Authorizer struct { | ||
enforcer *casbin.Enforcer | ||
} | ||
|
||
func (a *Authorizer) Authorize(subject, object, action string) error { | ||
if !a.enforcer.Enforce(subject, object, action) { | ||
msg := fmt.Sprintf( | ||
"%s not permitted to %s to %s", | ||
subject, | ||
action, | ||
object, | ||
) | ||
st := status.New(codes.PermissionDenied, msg) | ||
return st.Err() | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
var ( | ||
CAFile = configFile("ca.pem") | ||
ServerCertFile = configFile("server.pem") | ||
ServerKeyFile = configFile("server-key.pem") | ||
RootClientCertFile = configFile("root-client.pem") | ||
RootClientKeyFile = configFile("root-client-key.pem") | ||
NobodyClientCertFile = configFile("nobody-client.pem") | ||
NobodyClientKeyFile = configFile("nobody-client-key.pem") | ||
ACLModelFile = configFile("model.conf") | ||
ACLPolicyFile = configFile("policy.csv") | ||
) | ||
|
||
func configFile(filename string) string { | ||
if dir := os.Getenv("CONFIG_DIR"); dir != "" { | ||
return filepath.Join(dir, filename) | ||
} | ||
homeDir, err := os.UserHomeDir() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return filepath.Join(homeDir, ".proglog", filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package config | ||
|
||
import ( | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func SetupTLSConfig(cfg TLSConfig) (*tls.Config, error) { | ||
var err error | ||
tlsConfig := &tls.Config{} | ||
if cfg.CertFile != "" && cfg.KeyFile != "" { | ||
tlsConfig.Certificates = make([]tls.Certificate, 1) | ||
tlsConfig.Certificates[0], err = tls.LoadX509KeyPair( | ||
cfg.CertFile, | ||
cfg.KeyFile, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
if cfg.CAFile != "" { | ||
b, err := os.ReadFile(cfg.CAFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
ca := x509.NewCertPool() | ||
ok := ca.AppendCertsFromPEM([]byte(b)) | ||
if !ok { | ||
return nil, fmt.Errorf( | ||
"failed to parse root certificate: %q", | ||
cfg.CAFile, | ||
) | ||
} | ||
if cfg.Server { | ||
tlsConfig.ClientCAs = ca | ||
tlsConfig.ClientAuth = tls.RequireAndVerifyClientCert | ||
} else { | ||
tlsConfig.RootCAs = ca | ||
} | ||
tlsConfig.ServerName = cfg.ServerAddress | ||
} | ||
return tlsConfig, nil | ||
} | ||
|
||
type TLSConfig struct { | ||
CertFile string | ||
KeyFile string | ||
CAFile string | ||
ServerAddress string | ||
Server bool | ||
} |
Oops, something went wrong.