diff --git a/database/init_postgres.go b/database/init_postgres.go index 5c8fe53..96df32d 100644 --- a/database/init_postgres.go +++ b/database/init_postgres.go @@ -2,10 +2,8 @@ package database import ( "os" - "time" "github.com/PretendoNetwork/super-mario-maker/globals" - "github.com/lib/pq" ) func initPostgres() { @@ -157,125 +155,125 @@ func ensureEventCourseMetaDataFileExists() { // * ensures that it exists both in S3 and in the // * database before booting. It has the reserved // * DataID 900000 - bucket := os.Getenv("PN_SMM_CONFIG_S3_BUCKET") - key := "900000.bin" + // bucket := os.Getenv("PN_SMM_CONFIG_S3_BUCKET") + // key := "900000.bin" - objectSizeS3, err := globals.S3ObjectSize(bucket, key) - if err != nil { - globals.Logger.Errorf("Failed to stat event course metadata file. Ensure your S3 credentials are correct and the 900000.bin file is uploaded to your bucket. S3 error: %s", err.Error()) - os.Exit(0) - } + // objectSizeS3, err := globals.S3ObjectSize(bucket, key) + // if err != nil { + // globals.Logger.Errorf("Failed to stat event course metadata file. Ensure your S3 credentials are correct and the 900000.bin file is uploaded to your bucket. S3 error: %s", err.Error()) + // os.Exit(0) + // } - globals.Logger.Success("Event course metadata file found. Verifying database") + // globals.Logger.Success("Event course metadata file found. Verifying database") - var exists bool - err = Postgres.QueryRow(`SELECT EXISTS(SELECT 1 FROM datastore.objects WHERE data_id=900000) AS "exists"`).Scan(&exists) - if err != nil { - globals.Logger.Errorf("Error querying for event course metadata object in Postgres: %s", err.Error()) - os.Exit(0) - } + // var exists bool + // err = Postgres.QueryRow(`SELECT EXISTS(SELECT 1 FROM datastore.objects WHERE data_id=900000) AS "exists"`).Scan(&exists) + // if err != nil { + // globals.Logger.Errorf("Error querying for event course metadata object in Postgres: %s", err.Error()) + // os.Exit(0) + // } - now := time.Now() + // now := time.Now() - if !exists { - var dataID uint64 + // if !exists { + // var dataID uint64 - globals.Logger.Info("Event course metadata object not found in Postgres. Creating") + // globals.Logger.Info("Event course metadata object not found in Postgres. Creating") - err := Postgres.QueryRow(`INSERT INTO datastore.objects ( - data_id, - upload_completed, - owner, - size, - name, - data_type, - meta_binary, - permission, - permission_recipients, - delete_permission, - delete_permission_recipients, - flag, - period, - refer_data_id, - tags, - persistence_slot_id, - extra_data, - creation_date, - update_date - ) VALUES ( - $1, - $2, - $3, - $4, - $5, - $6, - $7, - $8, - $9, - $10, - $11, - $12, - $13, - $14, - $15, - $16, - $17, - $18, - $19 - ) RETURNING data_id`, - 900000, - true, - 2, // * "Quazal Rendez-Vous" special account - objectSizeS3, - "", // * Has no name - 50, // * Metadata file has DataType 50. Event courses have DataType 51 - []byte{}, // * No MetaBinary - 0, // * Accessible by everyone - pq.Array([]uint32{}), - 3, // * THE REAL SERVER HAS THIS SET TO 0, FOR EVERYONE. THAT'S SUPER INSECURE. - pq.Array([]uint32{}), - 0, - 64306, - 0, - pq.Array([]string{}), - 0, // * Unsure what the slot ID actually is - pq.Array([]string{}), - now, - now, - ).Scan(&dataID) - if err != nil { - globals.Logger.Errorf("Error creating event course metadata object: %s", err.Error()) - os.Exit(0) - } - } else { - var objectSizeDB uint32 + // err := Postgres.QueryRow(`INSERT INTO datastore.objects ( + // data_id, + // upload_completed, + // owner, + // size, + // name, + // data_type, + // meta_binary, + // permission, + // permission_recipients, + // delete_permission, + // delete_permission_recipients, + // flag, + // period, + // refer_data_id, + // tags, + // persistence_slot_id, + // extra_data, + // creation_date, + // update_date + // ) VALUES ( + // $1, + // $2, + // $3, + // $4, + // $5, + // $6, + // $7, + // $8, + // $9, + // $10, + // $11, + // $12, + // $13, + // $14, + // $15, + // $16, + // $17, + // $18, + // $19 + // ) RETURNING data_id`, + // 900000, + // true, + // 2, // * "Quazal Rendez-Vous" special account + // objectSizeS3, + // "", // * Has no name + // 50, // * Metadata file has DataType 50. Event courses have DataType 51 + // []byte{}, // * No MetaBinary + // 0, // * Accessible by everyone + // pq.Array([]uint32{}), + // 3, // * THE REAL SERVER HAS THIS SET TO 0, FOR EVERYONE. THAT'S SUPER INSECURE. + // pq.Array([]uint32{}), + // 0, + // 64306, + // 0, + // pq.Array([]string{}), + // 0, // * Unsure what the slot ID actually is + // pq.Array([]string{}), + // now, + // now, + // ).Scan(&dataID) + // if err != nil { + // globals.Logger.Errorf("Error creating event course metadata object: %s", err.Error()) + // os.Exit(0) + // } + // } else { + // var objectSizeDB uint32 - err := Postgres.QueryRow(`SELECT size FROM datastore.objects WHERE data_id=900000`).Scan(&objectSizeDB) - if err != nil { - globals.Logger.Errorf("Error querying event course metadata object size: %s", err.Error()) - os.Exit(0) - } + // err := Postgres.QueryRow(`SELECT size FROM datastore.objects WHERE data_id=900000`).Scan(&objectSizeDB) + // if err != nil { + // globals.Logger.Errorf("Error querying event course metadata object size: %s", err.Error()) + // os.Exit(0) + // } - if objectSizeS3 != uint64(objectSizeDB) { - globals.Logger.Success("Event course metadata object found in Postgres! Updating size") + // if objectSizeS3 != uint64(objectSizeDB) { + // globals.Logger.Success("Event course metadata object found in Postgres! Updating size") - _, err := Postgres.Exec(`UPDATE datastore.objects SET size=$1, update_date=$2 WHERE data_id=900000`, objectSizeS3, now) - if err != nil { - globals.Logger.Errorf("Error updating event course metadata object size: %s", err.Error()) - os.Exit(0) - } - } else { - globals.Logger.Success("Event course metadata object found in Postgres!") - } + // _, err := Postgres.Exec(`UPDATE datastore.objects SET size=$1, update_date=$2 WHERE data_id=900000`, objectSizeS3, now) + // if err != nil { + // globals.Logger.Errorf("Error updating event course metadata object size: %s", err.Error()) + // os.Exit(0) + // } + // } else { + // globals.Logger.Success("Event course metadata object found in Postgres!") + // } - globals.Logger.Info("Ensuring event course metadata object has correct delete permission") + // globals.Logger.Info("Ensuring event course metadata object has correct delete permission") - _, err = Postgres.Exec(`UPDATE datastore.objects SET delete_permission=3 WHERE data_id=900000`) - if err != nil { - globals.Logger.Errorf("Error updating event course metadata object delete permission: %s", err.Error()) - os.Exit(0) - } - } + // _, err = Postgres.Exec(`UPDATE datastore.objects SET delete_permission=3 WHERE data_id=900000`) + // if err != nil { + // globals.Logger.Errorf("Error updating event course metadata object delete permission: %s", err.Error()) + // os.Exit(0) + // } + // } globals.Logger.Success("Event course metadata object found and is up to date!") } diff --git a/globals/accounts.go b/globals/accounts.go index e2c759b..300a255 100644 --- a/globals/accounts.go +++ b/globals/accounts.go @@ -10,11 +10,6 @@ import ( var AuthenticationServerAccount *nex.Account var SecureServerAccount *nex.Account -func InitAccounts() { - AuthenticationServerAccount = nex.NewAccount(1, "Quazal Authentication", KerberosPassword) - SecureServerAccount = nex.NewAccount(2, "Quazal Rendez-Vous", KerberosPassword) -} - func AccountDetailsByPID(pid types.PID) (*nex.Account, *nex.Error) { if pid.Equals(AuthenticationServerAccount.PID) { return AuthenticationServerAccount, nil diff --git a/globals/globals.go b/globals/globals.go index aa5ebed..82152f0 100644 --- a/globals/globals.go +++ b/globals/globals.go @@ -10,7 +10,7 @@ import ( "google.golang.org/grpc/metadata" ) -var Logger = plogger.NewLogger() +var Logger *plogger.Logger var KerberosPassword = "password" // * Default password var AuthenticationServer *nex.PRUDPServer var AuthenticationEndpoint *nex.PRUDPEndPoint diff --git a/init.go b/init.go index 40d1710..d10cdee 100644 --- a/init.go +++ b/init.go @@ -8,17 +8,23 @@ import ( "strings" pb "github.com/PretendoNetwork/grpc/go/account" + "github.com/PretendoNetwork/plogger-go" "github.com/PretendoNetwork/super-mario-maker/database" "github.com/PretendoNetwork/super-mario-maker/globals" "github.com/joho/godotenv" - "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" + + "github.com/PretendoNetwork/nex-go/v2" + + //"github.com/minio/minio-go/v7" + //"github.com/minio/minio-go/v7/pkg/credentials" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/metadata" ) func init() { + globals.Logger = plogger.NewLogger() + var err error err = godotenv.Load() @@ -26,10 +32,10 @@ func init() { globals.Logger.Warning("Error loading .env file") } - s3Endpoint := os.Getenv("PN_SMM_CONFIG_S3_ENDPOINT") - s3AccessKey := os.Getenv("PN_SMM_CONFIG_S3_ACCESS_KEY") - s3AccessSecret := os.Getenv("PN_SMM_CONFIG_S3_ACCESS_SECRET") - s3SecureEnv := os.Getenv("PN_SMM_CONFIG_S3_SECURE") + // s3Endpoint := os.Getenv("PN_SMM_CONFIG_S3_ENDPOINT") + // s3AccessKey := os.Getenv("PN_SMM_CONFIG_S3_ACCESS_KEY") + // s3AccessSecret := os.Getenv("PN_SMM_CONFIG_S3_ACCESS_SECRET") + // s3SecureEnv := os.Getenv("PN_SMM_CONFIG_S3_SECURE") postgresURI := os.Getenv("PN_SMM_POSTGRES_URI") authenticationServerPort := os.Getenv("PN_SMM_AUTHENTICATION_SERVER_PORT") @@ -53,7 +59,8 @@ func init() { globals.KerberosPassword = string(kerberosPassword) - globals.InitAccounts() + globals.AuthenticationServerAccount = nex.NewAccount(1, "Quazal Authentication", globals.KerberosPassword) + globals.SecureServerAccount = nex.NewAccount(2, "Quazal Rendez-Vous", globals.KerberosPassword) if strings.TrimSpace(authenticationServerPort) == "" { globals.Logger.Error("PN_SMM_AUTHENTICATION_SERVER_PORT environment variable not set") @@ -108,7 +115,7 @@ func init() { globals.Logger.Warning("Insecure gRPC server detected. PN_SMM_ACCOUNT_GRPC_API_KEY environment variable not set") } - globals.GRPCAccountClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", accountGRPCHost, accountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials())) + globals.GRPCAccountClientConnection, err = grpc.NewClient(fmt.Sprintf("%s:%s", accountGRPCHost, accountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { globals.Logger.Criticalf("Failed to connect to account gRPC server: %v", err) os.Exit(0) @@ -119,24 +126,24 @@ func init() { "X-API-Key", accountGRPCAPIKey, ) - staticCredentials := credentials.NewStaticV4(s3AccessKey, s3AccessSecret, "") + // staticCredentials := credentials.NewStaticV4(s3AccessKey, s3AccessSecret, "") - s3Secure, err := strconv.ParseBool(s3SecureEnv) - if err != nil { - globals.Logger.Warningf("PN_SMM_CONFIG_S3_SECURE environment variable not set. Using default value: %t", true) - s3Secure = true - } + // s3Secure, err := strconv.ParseBool(s3SecureEnv) + // if err != nil { + // globals.Logger.Warningf("PN_SMM_CONFIG_S3_SECURE environment variable not set. Using default value: %t", true) + // s3Secure = true + // } - minIOClient, err := minio.New(s3Endpoint, &minio.Options{ - Creds: staticCredentials, - Secure: s3Secure, - }) - if err != nil { - panic(err) - } + // minIOClient, err := minio.New(s3Endpoint, &minio.Options{ + // Creds: staticCredentials, + // Secure: s3Secure, + // }) + // if err != nil { + // panic(err) + // } - globals.MinIOClient = minIOClient - globals.Presigner = globals.NewS3Presigner(globals.MinIOClient) + // globals.MinIOClient = minIOClient + // globals.Presigner = globals.NewS3Presigner(globals.MinIOClient) // * Connect to and setup databases database.ConnectPostgres() diff --git a/nex/register_common_secure_server_protocols.go b/nex/register_common_secure_server_protocols.go index 4c02b0b..3fa2e79 100644 --- a/nex/register_common_secure_server_protocols.go +++ b/nex/register_common_secure_server_protocols.go @@ -6,7 +6,6 @@ import ( "github.com/PretendoNetwork/nex-go/v2/types" datastorecommon "github.com/PretendoNetwork/nex-protocols-common-go/v2/datastore" securecommon "github.com/PretendoNetwork/nex-protocols-common-go/v2/secure-connection" - datastore "github.com/PretendoNetwork/nex-protocols-go/v2/datastore" datastoresmm "github.com/PretendoNetwork/nex-protocols-go/v2/datastore/super-mario-maker" secure "github.com/PretendoNetwork/nex-protocols-go/v2/secure-connection" datastore_db "github.com/PretendoNetwork/super-mario-maker/database/datastore" @@ -22,30 +21,6 @@ func registerCommonSecureProtocols() { return nil } - datastoreProtocol := datastore.NewProtocol() - commonDataStoreProtocol := datastorecommon.NewCommonProtocol(datastoreProtocol) - - commonDataStoreProtocol.SetMinIOClient(globals.MinIOClient) - commonDataStoreProtocol.S3Bucket = os.Getenv("PN_SMM_CONFIG_S3_BUCKET") - - commonDataStoreProtocol.GetObjectInfoByDataID = datastore_db.GetObjectInfoByDataID - commonDataStoreProtocol.GetObjectInfoByPersistenceTargetWithPassword = datastore_db.GetObjectInfoByPersistenceTargetWithPassword - commonDataStoreProtocol.GetObjectInfoByDataIDWithPassword = datastore_db.GetObjectInfoByDataIDWithPassword - commonDataStoreProtocol.GetObjectOwnerByDataID = datastore_db.GetObjectOwnerByDataID - commonDataStoreProtocol.GetObjectSizeByDataID = datastore_db.GetObjectSizeByDataID - - commonDataStoreProtocol.UpdateObjectPeriodByDataIDWithPassword = datastore_db.UpdateObjectPeriodByDataIDWithPassword - commonDataStoreProtocol.UpdateObjectMetaBinaryByDataIDWithPassword = datastore_db.UpdateObjectMetaBinaryByDataIDWithPassword - commonDataStoreProtocol.UpdateObjectDataTypeByDataIDWithPassword = datastore_db.UpdateObjectDataTypeByDataIDWithPassword - commonDataStoreProtocol.UpdateObjectUploadCompletedByDataID = datastore_db.UpdateObjectUploadCompletedByDataID - - commonDataStoreProtocol.InitializeObjectByPreparePostParam = datastore_db.InitializeObjectByPreparePostParam - commonDataStoreProtocol.InitializeObjectRatingWithSlot = datastore_db.InitializeObjectRatingWithSlot - commonDataStoreProtocol.RateObjectWithPassword = datastore_db.RateObjectWithPassword - commonDataStoreProtocol.DeleteObjectByDataID = datastore_db.DeleteObjectByDataID - - globals.DatastoreCommon = commonDataStoreProtocol - smmDatastore := datastoresmm.NewProtocol(globals.SecureEndpoint) smmDatastore.GetObjectInfos = nex_datastore_super_mario_maker.GetObjectInfos @@ -66,4 +41,29 @@ func registerCommonSecureProtocols() { smmDatastore.GetMetasWithCourseRecord = nex_datastore_super_mario_maker.GetMetasWithCourseRecord smmDatastore.CheckRateCustomRankingCounter = nex_datastore_super_mario_maker.CheckRateCustomRankingCounter smmDatastore.CTRPickUpCourseSearchObject = nex_datastore_super_mario_maker.CTRPickUpCourseSearchObject + + globals.SecureEndpoint.RegisterServiceProtocol(smmDatastore) + + commonDataStoreProtocol := datastorecommon.NewCommonProtocol(smmDatastore) + + commonDataStoreProtocol.SetMinIOClient(globals.MinIOClient) + commonDataStoreProtocol.S3Bucket = os.Getenv("PN_SMM_CONFIG_S3_BUCKET") + + commonDataStoreProtocol.GetObjectInfoByDataID = datastore_db.GetObjectInfoByDataID + commonDataStoreProtocol.GetObjectInfoByPersistenceTargetWithPassword = datastore_db.GetObjectInfoByPersistenceTargetWithPassword + commonDataStoreProtocol.GetObjectInfoByDataIDWithPassword = datastore_db.GetObjectInfoByDataIDWithPassword + commonDataStoreProtocol.GetObjectOwnerByDataID = datastore_db.GetObjectOwnerByDataID + commonDataStoreProtocol.GetObjectSizeByDataID = datastore_db.GetObjectSizeByDataID + + commonDataStoreProtocol.UpdateObjectPeriodByDataIDWithPassword = datastore_db.UpdateObjectPeriodByDataIDWithPassword + commonDataStoreProtocol.UpdateObjectMetaBinaryByDataIDWithPassword = datastore_db.UpdateObjectMetaBinaryByDataIDWithPassword + commonDataStoreProtocol.UpdateObjectDataTypeByDataIDWithPassword = datastore_db.UpdateObjectDataTypeByDataIDWithPassword + commonDataStoreProtocol.UpdateObjectUploadCompletedByDataID = datastore_db.UpdateObjectUploadCompletedByDataID + + commonDataStoreProtocol.InitializeObjectByPreparePostParam = datastore_db.InitializeObjectByPreparePostParam + commonDataStoreProtocol.InitializeObjectRatingWithSlot = datastore_db.InitializeObjectRatingWithSlot + commonDataStoreProtocol.RateObjectWithPassword = datastore_db.RateObjectWithPassword + commonDataStoreProtocol.DeleteObjectByDataID = datastore_db.DeleteObjectByDataID + + globals.DatastoreCommon = commonDataStoreProtocol } diff --git a/nex/secure.go b/nex/secure.go index be70e67..50db40a 100644 --- a/nex/secure.go +++ b/nex/secure.go @@ -10,11 +10,10 @@ import ( ) func StartSecureServer() { - serverBuildString = "branch:origin/project/nfs build:3_10_26_2006_0" - globals.SecureServer = nex.NewPRUDPServer() globals.SecureEndpoint = nex.NewPRUDPEndPoint(1) + globals.SecureEndpoint.IsSecureEndPoint = true globals.SecureEndpoint.ServerAccount = globals.SecureServerAccount globals.SecureEndpoint.AccountDetailsByPID = globals.AccountDetailsByPID globals.SecureEndpoint.AccountDetailsByUsername = globals.AccountDetailsByUsername