Skip to content

Commit

Permalink
NOISSUE - Separate Keto hosts for read and write (absmach#1563)
Browse files Browse the repository at this point in the history
* Separate keto hosts for read and write

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* update readme with new envars

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

* rename read connection name

Signed-off-by: Ivan Milosevic <iva@blokovi.com>

Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com>
Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
Signed-off-by: 0x6f736f646f <blackd0t@protonmail.com>
  • Loading branch information
3 people authored and rodneyosodo committed May 26, 2022
1 parent c135406 commit 96c6679
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 20 deletions.
4 changes: 4 additions & 0 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ default values.
| MF_AUTH_SECRET | String used for signing tokens | auth |
| MF_AUTH_LOGIN_TOKEN_DURATION | The login token expiration period | 10h |
| MF_JAEGER_URL | Jaeger server URL | localhost:6831 |
| MF_KETO_READ_REMOTE_HOST | Keto Read Host | mainflux-keto |
| MF_KETO_WRITE_REMOTE_HOST | Keto Write Host | mainflux-keto |
| MF_KETO_READ_REMOTE_PORT | Keto Read Port | 4466 |
| MF_KETO_WRITE_REMOTE_PORT | Keto Write Port | 4467 |

## Deployment

Expand Down
30 changes: 17 additions & 13 deletions cmd/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ const (
defServerCert = ""
defServerKey = ""
defJaegerURL = ""
defKetoHost = "mainflux-keto"
defKetoWritePort = "4467"
defKetoReadHost = "mainflux-keto"
defKetoWriteHost = "mainflux-keto"
defKetoReadPort = "4466"
defKetoWritePort = "4467"
defLoginDuration = "10h"

envLogLevel = "MF_AUTH_LOG_LEVEL"
Expand All @@ -71,9 +72,10 @@ const (
envServerCert = "MF_AUTH_SERVER_CERT"
envServerKey = "MF_AUTH_SERVER_KEY"
envJaegerURL = "MF_JAEGER_URL"
envKetoHost = "MF_KETO_HOST"
envKetoWritePort = "MF_KETO_WRITE_REMOTE_PORT"
envKetoReadHost = "MF_KETO_READ_REMOTE_HOST"
envKetoWriteHost = "MF_KETO_WRITE_REMOTE_HOST"
envKetoReadPort = "MF_KETO_READ_REMOTE_PORT"
envKetoWritePort = "MF_KETO_WRITE_REMOTE_PORT"
envLoginDuration = "MF_AUTH_LOGIN_TOKEN_DURATION"
)

Expand All @@ -87,7 +89,8 @@ type config struct {
serverKey string
jaegerURL string
resetURL string
ketoHost string
ketoReadHost string
ketoWriteHost string
ketoWritePort string
ketoReadPort string
loginDuration time.Duration
Expand Down Expand Up @@ -115,7 +118,7 @@ func main() {
dbTracer, dbCloser := initJaeger("auth_db", cfg.jaegerURL, logger)
defer dbCloser.Close()

readerConn, writerConn := initKeto(cfg.ketoHost, cfg.ketoReadPort, cfg.ketoWritePort, logger)
readerConn, writerConn := initKeto(cfg.ketoReadHost, cfg.ketoReadPort, cfg.ketoWriteHost, cfg.ketoWritePort, logger)

svc := newService(db, dbTracer, cfg.secret, logger, readerConn, writerConn, cfg.loginDuration)
errs := make(chan error, 2)
Expand Down Expand Up @@ -160,7 +163,8 @@ func loadConfig() config {
serverCert: mainflux.Env(envServerCert, defServerCert),
serverKey: mainflux.Env(envServerKey, defServerKey),
jaegerURL: mainflux.Env(envJaegerURL, defJaegerURL),
ketoHost: mainflux.Env(envKetoHost, defKetoHost),
ketoReadHost: mainflux.Env(envKetoReadHost, defKetoReadHost),
ketoWriteHost: mainflux.Env(envKetoWriteHost, defKetoWriteHost),
ketoReadPort: mainflux.Env(envKetoReadPort, defKetoReadPort),
ketoWritePort: mainflux.Env(envKetoWritePort, defKetoWritePort),
loginDuration: loginDuration,
Expand Down Expand Up @@ -192,20 +196,20 @@ func initJaeger(svcName, url string, logger logger.Logger) (opentracing.Tracer,
return tracer, closer
}

func initKeto(hostAddress, readPort, writePort string, logger logger.Logger) (readerConnection, writerConnection *grpc.ClientConn) {
checkConn, err := grpc.Dial(fmt.Sprintf("%s:%s", hostAddress, readPort), grpc.WithInsecure())
func initKeto(hostReadAddress, readPort, hostWriteAddress, writePort string, logger logger.Logger) (readerConnection, writerConnection *grpc.ClientConn) {
readConn, err := grpc.Dial(fmt.Sprintf("%s:%s", hostReadAddress, readPort), grpc.WithInsecure())
if err != nil {
logger.Error(fmt.Sprintf("Failed to dial %s:%s for Keto Read Service: %s", hostAddress, readPort, err))
logger.Error(fmt.Sprintf("Failed to dial %s:%s for Keto Read Service: %s", hostReadAddress, readPort, err))
os.Exit(1)
}

writeConn, err := grpc.Dial(fmt.Sprintf("%s:%s", hostAddress, writePort), grpc.WithInsecure())
writeConn, err := grpc.Dial(fmt.Sprintf("%s:%s", hostWriteAddress, writePort), grpc.WithInsecure())
if err != nil {
logger.Error(fmt.Sprintf("Failed to dial %s:%s for Keto Write Service: %s", hostAddress, writePort, err))
logger.Error(fmt.Sprintf("Failed to dial %s:%s for Keto Write Service: %s", hostWriteAddress, writePort, err))
os.Exit(1)
}

return checkConn, writeConn
return readConn, writeConn
}

func connectToDB(dbConfig postgres.Config, logger logger.Logger) *sqlx.DB {
Expand Down
5 changes: 3 additions & 2 deletions docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ MF_AUTH_SECRET=secret
MF_AUTH_LOGIN_TOKEN_DURATION="10h"

### Keto
MF_KETO_HOST=mainflux-keto
MF_KETO_WRITE_REMOTE_PORT=4467
MF_KETO_READ_REMOTE_HOST=mainflux-keto
MF_KETO_READ_REMOTE_PORT=4466
MF_KETO_WRITE_REMOTE_HOST=mainflux-keto
MF_KETO_WRITE_REMOTE_PORT=4467
MF_KETO_DB_PORT=5432
MF_KETO_DB_USER=mainflux
MF_KETO_DB_PASS=mainflux
Expand Down
10 changes: 5 additions & 5 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ services:
image: oryd/keto:v0.6.0-alpha.3
container_name: mainflux-keto
ports:
- ${MF_KETO_READ_REMOTE_PORT}:4466
- ${MF_KETO_WRITE_REMOTE_PORT}:4467
- ${MF_KETO_READ_REMOTE_PORT}:${MF_KETO_READ_REMOTE_PORT}
- ${MF_KETO_WRITE_REMOTE_PORT}:${MF_KETO_WRITE_REMOTE_PORT}
environment:
- DSN=postgresql://${MF_KETO_DB_USER}:${MF_KETO_DB_PASS}@keto-db:${MF_KETO_DB_PORT}/${MF_KETO_DB}?sslmode=disable
command: serve -c /home/ory/keto.yml
Expand Down Expand Up @@ -140,10 +140,10 @@ services:
MF_AUTH_SECRET: ${MF_AUTH_SECRET}
MF_AUTH_LOGIN_TOKEN_DURATION: ${MF_AUTH_LOGIN_TOKEN_DURATION}
MF_JAEGER_URL: ${MF_JAEGER_URL}
MF_KETO_HOST: ${MF_KETO_HOST}
MF_KETO_WRITE_REMOTE_PORT: ${MF_KETO_WRITE_REMOTE_PORT}
MF_KETO_READ_REMOTE_HOST: ${MF_KETO_READ_REMOTE_HOST}
MF_KETO_READ_REMOTE_PORT: ${MF_KETO_READ_REMOTE_PORT}

MF_KETO_WRITE_REMOTE_HOST: ${MF_KETO_WRITE_REMOTE_HOST}
MF_KETO_WRITE_REMOTE_PORT: ${MF_KETO_WRITE_REMOTE_PORT}
ports:
- ${MF_AUTH_HTTP_PORT}:${MF_AUTH_HTTP_PORT}
- ${MF_AUTH_GRPC_PORT}:${MF_AUTH_GRPC_PORT}
Expand Down

0 comments on commit 96c6679

Please sign in to comment.