diff --git a/src/CaosDB.jl b/src/CaosDB.jl index 970cd23018384b45a75349c1ac204fb418e6b809..9ad459b1a3f383fe6b96a964fd415f764cf07d3c 100644 --- a/src/CaosDB.jl +++ b/src/CaosDB.jl @@ -23,6 +23,25 @@ module CaosDB +module Info + +""" +Struct containing version information of the CaosDB server. +""" +mutable struct VersionInfo + + major::Cint + minor::Cint + patch::Cint + pre_release::Cstring + build::Cstring + + VersionInfo() = new() + +end + +end # Info + module Utility """ @@ -46,11 +65,286 @@ function get_env_var(var::AbstractString, default::AbstractString = "") end -end #Utility +end # Utility + +module Authentication + +""" +Struct containing a pointer to the wrapped cpp authenticator class. +""" +mutable struct Authenticator + wrapped_authenticator::Ptr{Cvoid} + + Authenticator() = new() +end + +function create_plain_password_authenticator( + username::AbstractString, + password::AbstractString, +) + + auth = Ref{Authenticator}(Authenticator()) + + err_code = ccall( + (:caosdb_authentication_create_plain_password_authenticator, "libccaosdb"), + Cint, + (Ref{Authenticator}, Cstring, Cstring), + auth, + username, + password, + ) + + if err_code != 0 + + @error "Creating authenticator failed with code $err_code" + + end + + return auth + +end + +end # Authentication + +module Connection + +using ..CaosDB + +""" +Struct containing the actual connection to a CaosDB server. +""" +mutable struct CaosDBConnection + wrapped_connection::Ptr{Cvoid} + + CaosDBConnection() = new() +end + +""" +Struct containing a pointer to the wrapped cpp class providing the +certificate provider. +""" +mutable struct CertificateProvider + wrapped_certificate_provider::Ptr{Cvoid} + + CertificateProvider() = new() +end + +""" +Struct containing a pointer to the wrapped cpp class for storing the +connection configuration. +""" +mutable struct Configuration + wrapped_connection_configuration::Ptr{Cvoid} + + Configuration() = new() +end + +""" + create_pem_file_certificate_provider(path::AbstractString) + +Return a `CertificateProvider` for the pem certificate located at +`path`. +""" +function create_pem_file_certificate_provider(path::AbstractString) + + cert_provider = Ref{CertificateProvider}(CertificateProvider()) + + err_code = ccall( + (:caosdb_connection_create_pem_file_certificate_provider, "libccaosdb"), + Cint, + (Ref{CertificateProvider}, Cstring), + cert_provider, + path, + ) + + if err_code != 0 + + @error "PEM certificate creation returned code $err_code." + + end + + return cert_provider + +end + +""" + create_tls_connection_configuration( + host::AbstractString, + port::Cint, + authenticator::Ref{CaosDB.Authentication.Authenticator}, + provider::Ref{CertificateProvider} + ) + +Return a TLS connection configuration with authentication. +""" +function create_tls_connection_configuration( + host::AbstractString, + port::Cint, + authenticator::Ref{CaosDB.Authentication.Authenticator}, + provider::Ref{CertificateProvider}, +) + + config = Ref{Configuration}(Configuration()) + + err_code = ccall( + (:caosdb_connection_create_tls_connection_configuration, "libccaosdb"), + Cint, + ( + Ref{Configuration}, + Cstring, + Cint, + Ref{CaosDB.Authentication.Authenticator}, + Ref{CertificateProvider}, + ), + config, + host, + port, + authenticator, + provider, + ) + + if err_code != 0 + + @error "TLS-configuration creation returned code $err_code." + + end + + return config + +end + +function create_insecure_connection_configuration(host::AbstractString, port::Cint) + + config = Ref{Configuration}(Configuration()) + + err_code = ccall( + (:caosdb_connection_create_insecure_connection_configuration, "libccaosdb"), + Cint, + (Ref{Configuration}, Cstring, Cint), + config, + host, + port, + ) + + if err_code != 0 + + @error "Insecure configuration creation returned code $err_code." + + end + + return config +end + +""" + create_connection(config::Ref{Configuration}) + +Return a connection based on the given `config`. +""" +function create_connection(config::Ref{Configuration}) + + connection = Ref{CaosDBConnection}(CaosDBConnection()) + + err_code = ccall( + (:caosdb_connection_create_connection, "libccaosdb"), + Cint, + (Ref{CaosDBConnection}, Ref{Configuration}), + connection, + config, + ) + + if err_code != 0 + + @error "Creating connection failed with code $err_code." + + end + + return connection + +end + +""" + get_version_info(con::Ref{CaosDBConnection}) + +Return the version of the CaosDB server that `con` is connected +to. +""" +function get_version_info(con::Ref{CaosDBConnection}) + + println("Entering get_version_info...") + info = Ref{CaosDB.Info.VersionInfo}(CaosDB.Info.VersionInfo()) + println("Calling c function") + err_code = ccall( + (:caosdb_connection_get_version_info, "libccaosdb"), + Cint, + (Ref{CaosDB.Info.VersionInfo}, Ref{CaosDBConnection}), + info, + con, + ) + println("Return code $err_code") + # TODO Real error-code handling + if err_code != 0 + + @error "Version info returned with code $err_code" + + end + + return info + +end + +function connect( + host::AbstractString = nothing, + port_str::AbstractString = nothing, + cacert::AbstractString = nothing, + username::AbstractString = nothing, + password::AbstractString = nothing, +) + + if host == nothing + + host = CaosDB.Utility.get_env_var("CAOSDB_SERVER_HOST", "localhost") + + end + + if port_str == nothing + + port = CaosDB.Utility.get_env_var("CAOSDB_SERVER_GRPC_PORT_HTTPS", "localhost") + + end + + port = parse(Cint, port_str) + + if cacert == nothing + + cacert = CaosDB.Utility.get_env_var("CAOSDB_SERVER_CERT") + + end + + if username == nothing + + username = CaosDB.Utility.get_env_var("CAOSDB_USER", "admin") + + end + + if password == nothing + + password = CaosDB.Utility.get_env_var("CAOSDB_PASSWORD", "caosdb") + + end + + provider = create_pem_file_certificate_provider(cacert) + authenticator = + CaosDB.Authentication.create_plain_password_authenticator(username, password) + config = create_tls_connection_configuration(host, port, authenticator, provider) + connection = create_connection(config) + + return connection + +end -module Connection end +export connect -module Authentication end +end # Connection module Entity end