Class: OpenSSL::Config
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Enumerable
|
|
Inherits: | Object |
Defined in: | ext/openssl/ossl_config.c |
Overview
Configuration for the openssl library.
Many system’s installation of openssl library will depend on your system configuration. See the value of DEFAULT_CONFIG_FILE for the location of the file for your host.
Constant Summary
-
DEFAULT_CONFIG_FILE =
# File 'ext/openssl/ossl_config.c', line 459path_str
Class Method Summary
-
.new(filename) ⇒ Config
constructor
Creates an instance of
Config
from the content of the file specified by filename. -
.parse(string) ⇒ Config
Parses a given string as a blob that contains configuration for
::OpenSSL
. -
.parse_config(io) ⇒ Hash
Parses the configuration data read from io and returns the whole content as a Hash.
Instance Method Summary
-
#[](section) ⇒ Hash
Gets all key-value pairs in a specific section from the current configuration.
-
#each {|section, key, value| ... }
Retrieves the section and its pairs for the current configuration.
-
#get_value(section, key) ⇒ String
Gets the value of key from the given section.
- #initialize_copy(other)
-
#inspect ⇒ String
String representation of this configuration object, including the class name and its sections.
-
#sections ⇒ array of string
Get the names of all sections in the current configuration.
-
#to_s ⇒ String
Gets the parsable form of the current configuration.
Constructor Details
.new(filename) ⇒ Config
Creates an instance of Config
from the content of the file specified by filename.
This can be used in contexts like OpenSSL::X509::ExtensionFactory.config=
This can raise ::IO
exceptions based on the access, or availability of the file. A ConfigError exception may be raised depending on the validity of the data being configured.
# File 'ext/openssl/ossl_config.c', line 132
static VALUE config_initialize(int argc, VALUE *argv, VALUE self) { CONF *conf = GetConfig(self); VALUE filename; /* 0-arguments call has no use-case, but is kept for compatibility */ rb_scan_args(argc, argv, "01", &filename); rb_check_frozen(self); if (!NIL_P(filename)) { BIO *bio = BIO_new_file(StringValueCStr(filename), "rb"); if (!bio) ossl_raise(eConfigError, "BIO_new_file"); config_load_bio(conf, bio); /* Consumes BIO */ } return self; }
Class Method Details
.parse(string) ⇒ Config
Parses a given string as a blob that contains configuration for ::OpenSSL
.
# File 'ext/openssl/ossl_config.c', line 81
static VALUE config_s_parse(VALUE klass, VALUE str) { VALUE obj = config_s_alloc(klass); CONF *conf = GetConfig(obj); BIO *bio; bio = ossl_obj2bio(&str); config_load_bio(conf, bio); /* Consumes BIO */ return obj; }
.parse_config(io) ⇒ Hash
Parses the configuration data read from io and returns the whole content as a Hash.
# File 'ext/openssl/ossl_config.c', line 103
static VALUE config_s_parse_config(VALUE klass, VALUE io) { VALUE obj, sections, ret; long i; obj = config_s_parse(klass, io); sections = config_get_sections(obj); ret = rb_hash_new(); for (i = 0; i < RARRAY_LEN(sections); i++) { VALUE section = rb_ary_entry(sections, i); rb_hash_aset(ret, section, config_get_section(obj, section)); } return ret; }
Instance Method Details
#[](section) ⇒ Hash
Gets all key-value pairs in a specific section from the current configuration.
Given the following configurating file being loaded:
config = OpenSSL::Config.load('foo.cnf')
#=> #<OpenSSL::Config sections=["default"]>
puts config.to_s
#=> [ default ]
# foo=bar
You can get a hash of the specific section like so:
config['default']
#=> {"foo"=>"bar"}
# File 'ext/openssl/ossl_config.c', line 223
static VALUE config_get_section(VALUE self, VALUE section) { CONF *conf = GetConfig(self); STACK_OF(CONF_VALUE) *sk; int i, entries; VALUE hash; hash = rb_hash_new(); StringValueCStr(section); if (!(sk = NCONF_get_section(conf, RSTRING_PTR(section)))) { ossl_clear_error(); return hash; } entries = sk_CONF_VALUE_num(sk); for (i = 0; i < entries; i++) { CONF_VALUE *entry = sk_CONF_VALUE_value(sk, i); rb_hash_aset(hash, rb_str_new_cstr(entry->name), rb_str_new_cstr(entry->value)); } return hash; }
#each {|section, key, value| ... }
Retrieves the section and its pairs for the current configuration.
config.each do |section, key, value|
# ...
end
# File 'ext/openssl/ossl_config.c', line 375
static VALUE config_each(VALUE self) { CONF *conf = GetConfig(self); RETURN_ENUMERATOR(self, 0, 0); lh_doall_arg((_LHASH *)conf->data, LHASH_DOALL_ARG_FN(each_conf_value), NULL); return self; }
#get_value(section, key) ⇒ String
Gets the value of key from the given section.
Given the following configurating file being loaded:
config = OpenSSL::Config.load('foo.cnf')
#=> #<OpenSSL::Config sections=["default"]>
puts config.to_s
#=> [ default ]
# foo=bar
You can get a specific value from the config if you know the section and key like so:
config.get_value('default','foo')
#=> "bar"
# File 'ext/openssl/ossl_config.c', line 184
static VALUE config_get_value(VALUE self, VALUE section, VALUE key) { CONF *conf = GetConfig(self); const char *str, *sectionp; StringValueCStr(section); StringValueCStr(key); /* For compatibility; NULL means "default". */ sectionp = RSTRING_LEN(section) ? RSTRING_PTR(section) : NULL; str = NCONF_get_string(conf, sectionp, RSTRING_PTR(key)); if (!str) { ossl_clear_error(); return Qnil; } return rb_str_new_cstr(str); }
#initialize_copy(other)
[ GitHub ]# File 'ext/openssl/ossl_config.c', line 150
static VALUE config_initialize_copy(VALUE self, VALUE other) { CONF *conf = GetConfig(self); VALUE str; BIO *bio; str = rb_funcall(other, rb_intern("to_s"), 0); rb_check_frozen(self); bio = ossl_obj2bio(&str); config_load_bio(conf, bio); /* Consumes BIO */ return self; }
#inspect ⇒ String
String representation of this configuration object, including the class name and its sections.
# File 'ext/openssl/ossl_config.c', line 394
static VALUE config_inspect(VALUE self) { VALUE str, ary = config_get_sections(self); const char *cname = rb_class2name(rb_obj_class(self)); str = rb_str_new_cstr("#<"); rb_str_cat_cstr(str, cname); rb_str_cat_cstr(str, " sections="); rb_str_append(str, rb_inspect(ary)); rb_str_cat_cstr(str, ">"); return str; }
#sections ⇒ array
of
string
Get the names of all sections in the current configuration.
# File 'ext/openssl/ossl_config.c', line 263
static VALUE config_get_sections(VALUE self) { CONF *conf = GetConfig(self); VALUE ary; ary = rb_ary_new(); lh_doall_arg((_LHASH *)conf->data, LHASH_DOALL_ARG_FN(get_conf_section), &ary); return ary; }
#to_s ⇒ String
Gets the parsable form of the current configuration.
Given the following configuration being created:
config = OpenSSL::Config.new
#=> #<OpenSSL::Config sections=[]>
config['default'] = {"foo"=>"bar","baz"=>"buz"}
#=> {"foo"=>"bar", "baz"=>"buz"}
puts config.to_s
#=> [ default ]
# foo=bar
# baz=buz
You can parse get the serialized configuration using #to_s
and then parse it later:
serialized_config = config.to_s
# much later...
new_config = OpenSSL::Config.parse(serialized_config)
#=> #<OpenSSL::Config sections=["default"]>
puts new_config
#=> [ default ]
foo=
baz=buz
# File 'ext/openssl/ossl_config.c', line 331
static VALUE config_to_s(VALUE self) { CONF *conf = GetConfig(self); VALUE str; str = rb_str_new(NULL, 0); lh_doall_arg((_LHASH *)conf->data, LHASH_DOALL_ARG_FN(dump_conf_value), &str); return str; }