how to connect postgres to azure ? (golang-azure) - postgresql

I have an application that has only the POST method, and every time I make a request in that application, it saves some data for me in my postgres database..
locally my application works properly, but when I deploy and try to test it through the azure function app it returns a 500 error and says that it was not possible to store my information in the database..
here are some code snippets
go code:
func (h handler) sendgridWeb(c *gin.Context) {
data, _ := io.ReadAll(c.Request.Body)
var ListaEventoEmail []Email
if err := json.Unmarshal(data, &ListaEventoEmail); err != nil {
log.Fatal("Error when unrealizing content contained in Body")
}
for _, email := range ListaEventoEmail {
email.SgMessageId = email.SgMessageId[:strings.IndexByte(email.SgMessageId, '.')]
//-- PROCESSED --//
if email.Event == "processed" {
inserting := `insert into email (email_destinatario, enviado, id_sendgrid) values ($1, $2, $3)`
_, err := h.DB.Exec(inserting, email.Email, true, email.SgMessageId)
if err != nil {
log.Fatal("Error inserting data into email table")
}
}
//-- DELIVERED --//
if email.Event == "delivered" {
_, err := h.DB.Exec(`UPDATE email SET recebido=true WHERE id_sendgrid=$1`, email.SgMessageId)
if err != nil {
log.Fatal("Error when uploading email with DELIVERED event")
}
//-- OPEN --//
} else if email.Event == "open" {
_, err := h.DB.Exec(`UPDATE email SET recebido=true, aberto=true WHERE id_sendgrid=$1`, email.SgMessageId)
if err != nil {
log.Fatal("Error when uploading email with OPEN event")
}
} else if email.Event == "click" {
_, err := h.DB.Exec(`UPDATE email SET click = click + 1 WHERE id_sendgrid=$1`, email.SgMessageId)
if err != nil {
log.Fatal("Error when uploading email with CLICK event")
}
}
my connection string is stored in a toml file..
connection string:
# filename: config.toml
[postgres]
host = "132.168.0.22"
port = 5432
user = "postgres"
password = "ivaneteJC"
dbname = "webhook"
you can see that, in the HOST field I put my IP (fictitious ip)..
in my logic, when deploying to azure, my connection string should point to my machine, so I used the ip, thus performing the necessary operations on my database, but this does not happen .. when making a POST request by the postman (using the endpoint that o was provided when deploying to the azure function app, I get a 500 error and in my logs I get the message "Error inserting data into the email table" which is an error described in my go app, I believe I made a mistake somewhere that has a connection to the database, can someone help me?
function.json:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
host.json:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
},
"customHandler": {
"description": {
"defaultExecutablePath": "main.exe",
"workingDirectory": "",
"arguments": []
},
"enableForwardingHttpRequest": true
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "custom"
}
}
I did as mentioned in a comment, I treated the error in an original way .. and the following error appeared:
"connectex: An attempt was made to access a socket in a way prohibited by its access permissions."
db:
type handler struct {
DB *sql.DB
}
type PostgreConfig struct {
Host string
Port int
User string
Password string
Dbname string
}
type MyConfig struct {
Postgres *PostgreConfig
}
func Init() *sql.DB {
doc, err := os.ReadFile("config.toml")
if err != nil {
panic(err)
}
var cfg MyConfig
err = toml.Unmarshal(doc, &cfg)
if err != nil {
panic(err)
}
stringConn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", cfg.Postgres.Host, cfg.Postgres.Port, cfg.Postgres.User, cfg.Postgres.Password, cfg.Postgres.Dbname)
db, err := sql.Open("postgres", stringConn)
if err != nil {
log.Fatal("Erro ao realizar conexão")
} else {
fmt.Println("Conectado")
}
fmt.Println(stringConn)
db.Ping()
return db
}
func OpenDB(DB *sql.DB) handler {
return handler{DB}
}

Related

How can I inherit bearer token from collection? (POSTMAN, Golang RESTAPI)

I am able to verify the JWT Token, by manually setting each individual request with the bearer token, but whenever I set them to inherit auth from parent, it returns Unauthorized, it says clearly that it's taking the auth from the name of the collection, and I've set the collection to the bearer token.
Here is the JWTMiddleware:
func JWTMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := strings.Split(r.Header.Get("Authorization"), "Bearer ")
if len(authHeader) != 2 {
fmt.Println("Malformed token")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Malformed Token"))
} else {
jwtToken := authHeader[1]
token, err := jwt.Parse(jwtToken, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(os.Getenv("JWT_SECRET")), nil
})
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
ctx := context.WithValue(r.Context(), "props", claims)
next.ServeHTTP(w, r.WithContext(ctx))
} else {
fmt.Println(err)
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte("Unauthorized"))
}
}
})
}
Here is how the token is generated:
func generateJWT() (JWTToken, error) {
signingKey := []byte(os.Getenv("JWT_SECRET"))
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"exp": time.Now().Add(time.Hour * 1 * 1).Unix(),
})
tokenString, err := token.SignedString(signingKey)
return JWTToken{tokenString}, err
}
Here is how my routes are structured:
var Routes = router.RoutePrefix{
"/generate",
[]router.Route{
router.Route{
"gen_token",
"GET",
"",
gen_token,
false,
},
router.Route{
"testToken",
"GET",
"/test",
testToken,
true,
},
},
}
I am considering setting a global variable that I can set as the token for testing purposes, what would the solution be?

MongoDB Golang Driver only update fields were provided value is not empty

I want to create a function to update a specific document in MongoDB by it's id but only update the fields if the new provided value is not the Go default.
This is the document struct which I store in MongoDB:
type User struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
Username string `json:"username" bson:"username"`
FirstName string `json:"firstName" bson:"first_name"`
LastName string `json:"lastName,omitempty" bson:"last_name,omitempty"`
Email string `json:"email" bson:"email"`
Password string `json:"password,omitempty" bson:"password"`
PhoneNumber string `json:"phoneNumber,omitempty" bson:"phone_number,omitempty"`
Picture string `json:"picture,omitempty" bson:"picture,omitempty"`
Role Role `json:"role" bson:"role"`
}
My update function gets the id of the user document to update and a user struct with only the fields provided which should get updated. So if only the username should get updated all other fields in the provided user struct will have their default value.
I now need to first check if for example the new username is not empty and only then include it on the new update document.
This is how I would solve it in javsacript. Is there a similar solution for Go?
{
...(username && { username: username }),
...(email && { email: email }),
...(firstname && { firstname: firstname }),
...(lastname && { lastname: lastname }),
...(phone && { phone: phone }),
...(picture && { picture: picture }),
},
This is my Update function:
func (us *userQuery) Update(userId string, u datastruct.User) (*datastruct.User, error) {
userCollection := DB.Collection(datastruct.UserCollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_id, err := primitive.ObjectIDFromHex(userId)
if err != nil {
return nil, err
}
update := bson.D{{
Key: "$set",
Value: bson.D{
{Key: "username", Value: u.Username}, // only include a field if provided value is not the default
{Key: "firstName", Value: u.FirstName},
{Key: "lastName", Value: u.LastName},
{Key: "email", Value: u.Email},
{Key: "password", Value: u.Password},
{Key: "phoneNumber", Value: u.PhoneNumber},
{Key: "picture", Value: u.Picture},
},
}}
var result datastruct.User
res, err := userCollection.UpdateByID(
ctx,
_id,
update,
)
if err != nil {
return nil, err
}
return &result, nil
}
You have to build the update clause dynamically:
value:=bson.M{}
if len(u.UserName)!=0 {
value["username"]=u.UserName
}
if len(u.FirstName)!=0 {
value["firstName"]=u.FirstName
}
...
if len(value)>0 { // Is there anything to update?
res, err := userCollection.UpdateByID(
ctx,
_id,
bson.M{"$set":value})
}

Add user to mongodb with mongodb go driver

I want to add a mongo user to the mongodb. I tried the following:
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"log"
"os"
"time"
)
type User struct {
User string `json:"user"`
Db string `json:"db"`
Roles []Role`json:"roles"`
PasswordDigestor string `json:"passwordDigestor"`
Pwd string `json:"pwd"`
}
type Role struct {
Role string `json:"role"`
Db string `json:"db"`
}
func CreateUser() {
client, err := mongo.NewClient(options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s#%s/test?authSource=%s&replicaSet=%s",
mongoConf.user,mongoConf.password,mongoConf.host,mongoConf.authDB,mongoConf.replicaset)))
if err != nil {
log.Fatal(err)
}
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
err = client.Connect(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(ctx)
//pass := password(8)
pass := "Test123!"
if _, err = client.Database("admin").Collection("system.users").InsertOne(context.Background(),User{
User: "test",
Db: "admin",
Roles: []Role{
{Role: "readWriteAnyDatabase",
Db: "admin",
},
},
PasswordDigestor: "server",
Pwd: pass,
}); err != nil {
panic(err)
}
}
But that is not working. With the shell it is not a problem with db.createUser(). But how I'm supposed to do this with go? I was able to list all users from the system.users table. When I insert a user like this into the database its working without problems, but it is not possible to log in with the created user.
EDIT:
runnint db.createUser:
function (userObj, writeConcern) {
var name = userObj["user"];
var cmdObj = {createUser: name};
cmdObj = Object.extend(cmdObj, userObj);
delete cmdObj["user"];
this._modifyCommandToDigestPasswordIfNecessary(cmdObj, name);
cmdObj["writeConcern"] = writeConcern ? writeConcern : _defaultWriteConcern;
var res = this.runCommand(cmdObj);
if (res.ok) {
print("Successfully added user: " + getUserObjString(userObj));
return;
}
if (res.errmsg == "no such cmd: createUser") {
throw Error("'createUser' command not found. This is most likely because you are " +
"talking to an old (pre v2.6) MongoDB server");
}
if (res.errmsg == "timeout") {
throw Error("timed out while waiting for user authentication to replicate - " +
"database will not be fully secured until replication finishes");
}
throw _getErrorWithCode(res, "couldn't add user: " + res.errmsg);
}
Thanks to #Joe I could solve the problem running the following command:
r := client.Database(dbName).RunCommand(context.Background(),bson.D{{"createUser", userName},
{"pwd", pass}, {"roles", []bson.M{{"role": roleName,"db":roldeDB}}}})
if r.Err() != nil {
panic(r.Err())
}

Firebase Authentication creates user but does not add their Info to database

I am new to using firebase and ios development in general. I am having an issue with adding user's info to the firestore database even though they are being added as authenticated users. Any Suggestions?
Auth.auth().createUser(withEmail: email, password: password) { (result, err) in
if err != nil {
self.errorLabel.text = "Error Creating User"
self.errorLabel.alpha = 1
} else {
let db = Firestore.firestore()
db.collection("users").addDocument(data: ["firstname":firstname, "lastname":lastname, "uid":result!.user.uid]) { (error) in
if error != nil {
self.errorLabel.text = "error saving user data"
self.errorLabel.alpha = 1
}
}
self.transitionScreens()
}
}
}
}
Change your code to the following:
// Add a new document with a generated ID
var ref: DocumentReference? = nil
ref = db.collection("users").addDocument(data: [
"firstname": firstname,
"lastname": lastname,
"uid": result!.user.uid
]) { err in
if let err = err {
print("Error adding document: \(err)")
} else {
print("Document added with ID: \(ref!.documentID)")
}
}
Using this print statement print("Error adding document: \(err)") you can know exactly what the error is.
Also change your security rules to the following:
// Allow read/write access to all users under any conditions
// Warning: **NEVER** use this rule set in production; it allows
// anyone to overwrite your entire database.
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}
}
}
Check out the following different rules you can give access to users depending on the data
service cloud.firestore {
match /databases/{database}/documents {
//allows all users to read and write, but dangerous as any one can flood your database
match /public_collection/{document=**} {
allow read, write: if true;
}
//only read access
match /public_read_collection/{document=**} {
allow read: if true;
allow write: if false;
}
//prefered for storing users personal info, users can access only their data
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
}
//any authenticated user can access or write data
match /posts/{documentId} {
allow read, write: if request.auth.uid != null;
}
}
}

How to access JWT claims from API handler functions in go-swagger?

I'm using go-swagger with BearerAuth using JWT tokens. Along with the actual token I'm receiving claims which include such data as username.
How can I access claims in api.ItemsCreateItemHandler function below?
package restapi
func configureAPI(api *operations.MyAPI) http.Handler {
api.BearerAuth = func(token string) (interface{}, error) {
jwtToken := strings.Replace(token, "Bearer ", "", -1)
// skipped token verification
claims, _ := parsedToken.Claims.(jwt.MapClaims)
}
api.ItemsCreateItemHandler = items.CreateItemHandlerFunc(func(params items.CreateItemParams, principal interface{}) middleware.Responder {
// FIXME: Here I need to be able to access JWT claims
if err := createItem(params.Body, claims); err != nil {
return nil // handle error
}
return items.NewCreateItemCreated()
})
}
First your BearerAuth implementation is meant to return the security principal (which can be your claims in this case), this value will subsequently be passed to your handler in the principal argument.
So the way to do this is:
package restapi
import (
jwt "github.com/dgrijalva/jwt-go"
// ...
)
func configureAPI(api *operations.MyAPI) http.Handler {
api.BearerAuth = func(token string) (interface{}, error) {
jwtToken := strings.Replace(token, "Bearer ", "", -1)
// skipped token verification
claims, _ := parsedToken.Claims.(jwt.MapClaims)
return claims, nil
}
api.ItemsCreateItemHandler = items.CreateItemHandlerFunc(func(params items.CreateItemParams, principal interface{}) middleware.Responder {
claims, ok := principal.(jwt.MapClaims)
if !ok {
// handle error
}
if err := createItem(params.Body, claims); err != nil {
return nil // handle error
}
return items.NewCreateItemCreated()
})
}
You can make this less cumbersome by using the --principal jwt.MapClaims option to swagger generate so that it uses this type for the claims instead of interface{}.
A JWT is made up of 3 parts, split by punctuation - the token itself is base64 encoded.
For example, heres a token from https://jwt.io/
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
You'll want to split and decode, you're after the second part which contains the payload which is just plain JSON.
So in psuedo code it'll look something like;
json = Base64Decode(split(".", yourData)[1])
You'll see more concrete example at https://jwt.io/
I have a class that store token and contains parse method:
import io.jsonwebtoken.*;
#AllArgsConstructor
public class RawAccessJwtToken implements JwtToken {
private String token;
#Override
public String getToken() {
return token;
}
public Jws<Claims> parseClaims(String signingKey) {
try {
return Jwts.parser().setSigningKey(signingKey).parseClaimsJws(this.token);
} catch (UnsupportedJwtException | MalformedJwtException | IllegalArgumentException e) {
throw new BadCredentialsException("Invalid JWT token: " + e);
} catch (ExpiredJwtException expiredException){
throw new JwtExpiredTokenException(this, "JWT Token expired", expiredException);
}
}
}
With that class I can extract my users role:
Jws<Claims> claimsJws = token.parseClaims(signingKey);
List<String> scopes = claimsJws.getBody().get("scopes",
List.class);