Skip to content
Snippets Groups Projects
Commit fec47721 authored by florian's avatar florian
Browse files

DRAFT: Add basic funtionality (broken)

parent c959156a
No related branches found
No related tags found
2 merge requests!3TST: Add CI infrastructure for integration testing,!2ENH: Add minimal wrapping of libcaosdb's C interface
Pipeline #10260 passed
...@@ -23,6 +23,25 @@ ...@@ -23,6 +23,25 @@
module CaosDB 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 module Utility
""" """
...@@ -48,9 +67,284 @@ end ...@@ -48,9 +67,284 @@ end
end # Utility end # Utility
module Connection end 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
export connect
module Authentication end end # Connection
module Entity end module Entity end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment