I am using a postgres database. My api uses golang(gorm package for talking to postgres), i want to connect to postgres trough zitiContext. Any ideas/examples if that is possible(It works fine with nats and custom dialers)?
You don't have a project in git somewhere I could clone and look at do you? If you have an example repo, it'd probably be best. Otherwise I'll just look at one of their examples to see how it might work.
Here's some code from gorm docs(On how to connect to database, see postgres part): Connecting to a Database | GORM - The fantastic ORM library for Golang, aims to be developer friendly.
I gave it a few hours yesterday and I wasn't able to find the proper combination of pgx/driver/gorm combination to satisfy gorm.
I was able to get postgres to work without a problem, but with the time I gave it, I wasn't able bridge the gap into gorm....
Here's an incredibly simple example showing you how to use postgres without gorm. Maybe you can find the missing link between gorm, pgx, and the standard libraries? If you do let us know! I will try to come back to this some day since it'll be interesting to have an answer to this. There's no error handling or best practices here, just some quick code showing you how it works. Quick note that I use an 'addressable terminator' here so you specify which host to target using the dial options, not using a service.
Hope that helps - let us know how you get along!
zctx, _ := ziti.NewContextFromFile(`path/to/pclient.json`)
pgconfig, _ := pgconn.ParseConfig("host=127.0.0.1 user=postgres password=pg dbname=postgres port=5432 sslmode=disable")
pgconfig.DialFunc = func(ctx context.Context, network, addr string) (net.Conn, error) {
return zctx.DialWithOptions("private_postgres", &ziti.DialOptions{Identity: "ip-172-31-47-200-edge-router"})
}
conn, err := pgconn.ConnectConfig(context.Background(), pgconfig)
if err != nil {
log.Fatalf("err: %v", err)
}
//obviousl the table 'data' needs to exist :)
conn.Exec(context.Background(), "INSERT INTO data (name) VALUES ('bob');")
I create the service/identities/config with the ziti cli like this:
# establish some variables which are used below
service_name=private_postgres
client_identity="pclient"
the_port=5432
ziti edge create config "${service_name}.host.v1" host.v1 \
'{"protocol":"tcp", "address":"localhost","port":'"${the_port}"', "listenOptions": {"bindUsingEdgeIdentity":true}}'
# intercept is not needed for zscp/zssh but make it for testing if you like
ziti edge create config "${service_name}.intercept.v1" intercept.v1 \
'{"protocols":["tcp"],"addresses":["'"${service_name}.ziti"'"], "portRanges":[{"low":'"${the_port}"', "high":'"${the_port}"'}]}'
ziti edge create service "${service_name}" \
--configs "${service_name}.intercept.v1","${service_name}.host.v1"
ziti edge create service-policy "${service_name}-binding" Bind \
--service-roles "@${service_name}" --identity-roles "#${service_name}.binders" --semantic "AnyOf"
ziti edge create service-policy "${service_name}-dialing" Dial \
--service-roles "@${service_name}" --identity-roles "#${service_name}.dialers" --semantic "AnyOf"
# create two identities. one host - one client. Only necessary if you want/need them. Skippable if you have identities already
ziti edge create identity "${client_identity}" \
-a "${service_name}.dialers" -o "${client_identity}.jwt"
ziti edge enroll "${client_identity}.jwt"