class OpenSSL::Config
用于 openssl 库的配置。
许多系统安装的 openssl 库将依赖于您的系统配置。查看 OpenSSL::Config::DEFAULT_CONFIG_FILE 的值,以获取您的主机文件的位置。
常量
- DEFAULT_CONFIG_FILE
-
OpenSSL的默认系统配置文件。
公共类方法
源码
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 */
}
rb_obj_freeze(self);
return self;
}
从 filename 指定的文件内容创建 OpenSSL::Config 的一个实例。
这可以用于诸如 OpenSSL::X509::ExtensionFactory.config= 之类的上下文中。
这可能会引发基于文件访问或可用性的 IO 异常。根据配置数据的有效性,可能会引发 ConfigError 异常。
源码
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 */
rb_obj_freeze(obj);
return obj;
}
将给定的 string 解析为一个包含 OpenSSL 配置的 blob。
源码
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;
}
解析从 io 读取的配置数据,并将整个内容作为 Hash 返回。
公共实例方法
源码
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;
}
从当前配置的特定 section 中获取所有键值对。
假设加载了以下配置文件
config = OpenSSL::Config.load('foo.cnf') #=> #<OpenSSL::Config sections=["default"]> puts config.to_s #=> [ default ] # foo=bar
您可以像这样获取特定 section 的哈希值
config['default'] #=> {"foo"=>"bar"}
源码
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;
}
检索当前配置的 section 及其键值对。
config.each do |section, key, value| # ... end
源码
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);
}
从给定的 section 中获取 key 的值。
假设加载了以下配置文件
config = OpenSSL::Config.load('foo.cnf') #=> #<OpenSSL::Config sections=["default"]> puts config.to_s #=> [ default ] # foo=bar
如果您知道 section 和 key,您可以像这样从配置中获取特定的值
config.get_value('default','foo') #=> "bar"
源码
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 */
rb_obj_freeze(self);
return self;
}
源码
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;
}
此配置对象的 String 表示形式,包括类名及其 sections。
源码
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;
}
获取当前配置中所有 sections 的名称。
源码
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;
}
获取当前配置的可解析形式。
假设加载了以下配置文件
config = OpenSSL::Config.load('baz.cnf') #=> #<OpenSSL::Config sections=["default"]> puts config.to_s #=> [ default ] # foo=bar # baz=buz
您可以使用 to_s 获取序列化的配置,然后在以后解析它
serialized_config = config.to_s # much later... new_config = OpenSSL::Config.parse(serialized_config) #=> #<OpenSSL::Config sections=["default"]> puts new_config #=> [ default ] foo=bar baz=buz