| 1 |
|
| /* punk_config.h - configuration: layered YAML, resolved secrets. |
| 2 |
|
| * |
| 3 |
|
| * Boot-time work, in C for the same reason the rest of Punk is: one |
| 4 |
|
| * implementation, one place. YAML parsing itself is the exception - it |
| 5 |
|
| * goes through YAML::XS with a single call per file (the shim Open::API |
| 6 |
|
| * uses), which at once-per-boot costs nothing worth a parser of our own. |
| 7 |
|
| * |
| 8 |
|
| * The rule the design follows: a config file says WHERE a secret comes |
| 9 |
|
| * from, never what it is. A reference is a single-key hash whose key |
| 10 |
|
| * starts with '$' - a shape nothing else in a config has, and one that |
| 11 |
|
| * needs nothing from the parser: |
| 12 |
|
| * |
| 13 |
|
| * password: { $env: DB_PASSWORD } |
| 14 |
|
| * |
| 15 |
|
| * Resolved values land in `secrets` (dotted path -> value) and in |
| 16 |
|
| * `resolved`; `public` is the same tree with every secret replaced by |
| 17 |
|
| * [redacted], so it can be logged or serialised safely. |
| 18 |
|
| */ |
| 19 |
|
| |
| 20 |
|
| #ifndef PUNK_CONFIG_H |
| 21 |
|
| #define PUNK_CONFIG_H |
| 22 |
|
| |
| 23 |
|
| typedef struct punk_config { |
| 24 |
|
| HV *public; /* redacted copy */ |
| 25 |
|
| HV *resolved; /* real values */ |
| 26 |
|
| HV *secrets; /* dotted path -> resolved value */ |
| 27 |
|
| AV *files; /* the layers actually read */ |
| 28 |
|
| SV *file; /* the base path as given */ |
| 29 |
|
| SV *env; /* environment name */ |
| 30 |
|
| } punk_config; |
| 31 |
|
| |
| 32 |
166 |
| static punk_config *punk_config_of(pTHX_ SV *self) { |
| 33 |
166 |
▶ | if (!SvROK(self) || !SvIOK(SvRV(self))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
166 |
0 |
166 |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
0 |
166 |
166 |
|
| 34 |
0 |
| croak("Punk::Config: not a config handle"); |
| 35 |
166 |
| return (punk_config *)INT2PTR(void *, SvIV(SvRV(self))); |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
|
| /* ---- YAML ---------------------------------------------------------------- */ |
| 39 |
|
| |
| 40 |
|
| /* One document through YAML::XS. The shim localises the boolean class so |
| 41 |
|
| * true/false decode like JSON booleans, the way Open::API does it. */ |
| 42 |
65 |
| static SV *pc_yaml_decode(pTHX_ SV *text, const char *path) { |
| 43 |
65 |
| dSP; |
| 44 |
|
| int count; |
| 45 |
|
| SV *ret; |
| 46 |
|
| static int shim = 0; |
| 47 |
65 |
▶ | if (!pk_require_once(aTHX_ "YAML::XS", FALSE)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pk_require_once(aTHX_ "YAML::XS", FALSE)) |
0 |
65 |
65 |
|
| 48 |
0 |
| croak("Punk::Config: reading '%s' needs YAML::XS (not installed)", |
| 49 |
|
| path); |
| 50 |
65 |
▶ | if (!shim) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shim) { |
29 |
36 |
65 |
|
| 51 |
29 |
| eval_pv( |
| 52 |
|
| "sub Punk::Config::_yaml_load {" |
| 53 |
|
| " local $YAML::XS::Boolean =" |
| 54 |
|
| " eval { require JSON::PP; 1 } ? 'JSON::PP' : $YAML::XS::Boolean;" |
| 55 |
|
| /* a config file is data; an object tag in it must not bless */ |
| 56 |
|
| " local $YAML::XS::LoadBlessed = 0;" |
| 57 |
|
| " YAML::XS::Load($_[0]);" |
| 58 |
|
| "}", TRUE); |
| 59 |
29 |
| shim = 1; |
| 60 |
|
| } |
| 61 |
|
| /* Both eval_pv calls above can grow (and so reallocate) the value stack; |
| 62 |
|
| * SP was captured before them, and the push below would write through |
| 63 |
|
| * it. */ |
| 64 |
65 |
| SPAGAIN; |
| 65 |
65 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
65 |
65 |
|
| 66 |
65 |
▶ | XPUSHs(text); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(text); |
0 |
65 |
65 |
|
| 67 |
65 |
| PUTBACK; |
| 68 |
65 |
| count = call_pv("Punk::Config::_yaml_load", G_SCALAR | G_EVAL); |
| 69 |
65 |
| SPAGAIN; |
| 70 |
65 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
65 |
0 |
65 |
| if (SvTRUE(ERRSV)) { |
0 |
65 |
65 |
|
| 71 |
0 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 72 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 73 |
0 |
| croak("Punk::Config: '%s' is not valid YAML: %s", path, |
| 74 |
|
| SvPV_nolen(sv_2mortal(e))); |
| 75 |
|
| } |
| 76 |
65 |
▶ | ret = count > 0 ? newSVsv(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| ret = count > 0 ? newSVsv(POPs) : &PL_sv_undef; |
65 |
0 |
65 |
|
| 77 |
65 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
65 |
0 |
65 |
|
| 78 |
65 |
| return sv_2mortal(ret); |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| /* Slurp a file into a mortal SV, or NULL when it cannot be opened. */ |
| 82 |
176 |
| static SV *pc_slurp(pTHX_ const char *path) { |
| 83 |
176 |
| PerlIO *fh = PerlIO_open(path, "rb"); |
| 84 |
|
| SV *sv; |
| 85 |
|
| char buf[8192]; |
| 86 |
|
| SSize_t n; |
| 87 |
176 |
▶ | if (!fh) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fh) return NULL; |
110 |
66 |
176 |
|
| 88 |
66 |
| sv = sv_2mortal(newSVpvs("")); |
| 89 |
132 |
▶ | while ((n = PerlIO_read(fh, buf, sizeof buf)) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((n = PerlIO_read(fh, buf, sizeof buf)) > 0) |
66 |
66 |
132 |
|
| 90 |
66 |
| sv_catpvn(sv, buf, (STRLEN)n); |
| 91 |
66 |
| PerlIO_close(fh); |
| 92 |
66 |
| return sv; |
| 93 |
|
| } |
| 94 |
|
| |
| 95 |
|
| /* ---- merge --------------------------------------------------------------- |
| 96 |
|
| * Later layers win. Hashes merge key by key; anything else replaces, so an |
| 97 |
|
| * array in an overlay is a replacement rather than an append. */ |
| 98 |
|
| /* How deep a document may nest. A YAML alias cycle is unbounded, and the |
| 99 |
|
| * walk below copies subtrees, so an alias bomb is exponential: both end |
| 100 |
|
| * here with a message instead of a stack or the machine. */ |
| 101 |
|
| #define PC_MAX_DEPTH 64 |
| 102 |
|
| |
| 103 |
17 |
| static SV *pc_merge(pTHX_ SV *base, SV *over, int depth) { |
| 104 |
|
| HV *bh, *oh, *out; |
| 105 |
17 |
▶ | if (depth > PC_MAX_DEPTH) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth > PC_MAX_DEPTH) |
0 |
17 |
17 |
|
| 106 |
0 |
| croak("Punk::Config: nested deeper than %d levels (an alias " |
| 107 |
|
| "cycle?)", PC_MAX_DEPTH); |
| 108 |
|
| HE *he; |
| 109 |
17 |
▶ | if (!(base && SvROK(base) && SvTYPE(SvRV(base)) == SVt_PVHV |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(base && SvROK(base) && SvTYPE(SvRV(base)) == SVt_PVHV |
17 |
0 |
17 |
| if (!(base && SvROK(base) && SvTYPE(SvRV(base)) == SVt_PVHV |
13 |
4 |
17 |
| if (!(base && SvROK(base) && SvTYPE(SvRV(base)) == SVt_PVHV |
12 |
1 |
13 |
|
| 110 |
12 |
▶ | && over && SvROK(over) && SvTYPE(SvRV(over)) == SVt_PVHV)) |
Branch: 3 branches
| Branch | True | False | Total |
| && over && SvROK(over) && SvTYPE(SvRV(over)) == SVt_PVHV)) |
12 |
0 |
12 |
| && over && SvROK(over) && SvTYPE(SvRV(over)) == SVt_PVHV)) |
12 |
0 |
12 |
| && over && SvROK(over) && SvTYPE(SvRV(over)) == SVt_PVHV)) |
0 |
12 |
12 |
|
| 111 |
5 |
| return newSVsv(over); |
| 112 |
12 |
| bh = (HV *)SvRV(base); |
| 113 |
12 |
| oh = (HV *)SvRV(over); |
| 114 |
12 |
| out = newHV(); |
| 115 |
12 |
| hv_iterinit(bh); |
| 116 |
36 |
▶ | while ((he = hv_iternext(bh))) |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(bh))) |
24 |
12 |
36 |
|
| 117 |
24 |
▶ | (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
Branch: 2 branches
| Branch | True | False | Total |
| (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
24 |
0 |
24 |
| (void)hv_store_ent(out, HeSVKEY_force(he), newSVsv(HeVAL(he)), 0); |
0 |
24 |
24 |
|
| 118 |
12 |
| hv_iterinit(oh); |
| 119 |
25 |
▶ | while ((he = hv_iternext(oh))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(oh))) { |
13 |
12 |
25 |
|
| 120 |
13 |
▶ | SV *key = HeSVKEY_force(he); |
Branch: 2 branches
| Branch | True | False | Total |
| SV *key = HeSVKEY_force(he); |
13 |
0 |
13 |
| SV *key = HeSVKEY_force(he); |
0 |
13 |
13 |
|
| 121 |
13 |
| HE *have = hv_fetch_ent(out, key, 0, 0); |
| 122 |
9 |
| SV *merged = have ? pc_merge(aTHX_ HeVAL(have), HeVAL(he), depth + 1) |
| 123 |
13 |
▶ | : newSVsv(HeVAL(he)); |
Branch: 1 branch
| Branch | True | False | Total |
| : newSVsv(HeVAL(he)); |
9 |
4 |
13 |
|
| 124 |
13 |
| (void)hv_store_ent(out, key, merged, 0); |
| 125 |
|
| } |
| 126 |
12 |
| return newRV_noinc((SV *)out); |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
|
| /* ---- secret references --------------------------------------------------- |
| 130 |
|
| * A single-key hash whose key starts with '$'. Returns the key SV (borrowed) |
| 131 |
|
| * or NULL, setting *arg to the value. */ |
| 132 |
360 |
| static SV *pc_reference(pTHX_ SV *node, SV **arg) { |
| 133 |
|
| HV *hv; |
| 134 |
|
| HE *he; |
| 135 |
|
| SV *key; |
| 136 |
360 |
▶ | if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) return NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) return NULL; |
360 |
0 |
360 |
| if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) return NULL; |
250 |
110 |
360 |
| if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) return NULL; |
11 |
239 |
250 |
|
| 137 |
239 |
| hv = (HV *)SvRV(node); |
| 138 |
239 |
▶ | if (HvUSEDKEYS(hv) != 1) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (HvUSEDKEYS(hv) != 1) return NULL; |
0 |
239 |
239 |
| if (HvUSEDKEYS(hv) != 1) return NULL; |
66 |
173 |
239 |
|
| 139 |
173 |
| hv_iterinit(hv); |
| 140 |
173 |
| he = hv_iternext(hv); |
| 141 |
173 |
▶ | if (!he) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!he) return NULL; |
0 |
173 |
173 |
|
| 142 |
173 |
▶ | key = HeSVKEY_force(he); |
Branch: 2 branches
| Branch | True | False | Total |
| key = HeSVKEY_force(he); |
173 |
0 |
173 |
| key = HeSVKEY_force(he); |
0 |
173 |
173 |
|
| 143 |
173 |
▶ | if (SvCUR(key) < 2 || *SvPVX(key) != '$') return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvCUR(key) < 2 || *SvPVX(key) != '$') return NULL; |
106 |
67 |
173 |
| if (SvCUR(key) < 2 || *SvPVX(key) != '$') return NULL; |
92 |
14 |
106 |
|
| 144 |
14 |
| *arg = HeVAL(he); |
| 145 |
14 |
| return key; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
|
| /* Trim one trailing newline, the way a secret file or a command's output |
| 149 |
|
| * carries one. */ |
| 150 |
2 |
| static void pc_chomp(pTHX_ SV *sv) { |
| 151 |
|
| STRLEN len; |
| 152 |
|
| char *p; |
| 153 |
2 |
▶ | if (!SvOK(sv)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(sv)) return; |
0 |
2 |
2 |
|
| 154 |
2 |
| p = SvPV(sv, len); |
| 155 |
2 |
▶ | if (len && p[len - 1] == '\n') len--; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len && p[len - 1] == '\n') len--; |
2 |
0 |
2 |
| if (len && p[len - 1] == '\n') len--; |
2 |
0 |
2 |
|
| 156 |
2 |
▶ | if (len && p[len - 1] == '\r') len--; |
Branch: 2 branches
| Branch | True | False | Total |
| if (len && p[len - 1] == '\r') len--; |
2 |
0 |
2 |
| if (len && p[len - 1] == '\r') len--; |
0 |
2 |
2 |
|
| 157 |
2 |
| SvCUR_set(sv, len); |
| 158 |
2 |
| p[len] = '\0'; |
| 159 |
|
| } |
| 160 |
|
| |
| 161 |
|
| /* Resolve one reference. Returns an owned SV, or croaks - a secret that |
| 162 |
|
| * cannot be fetched must stop the boot, never become an empty password. */ |
| 163 |
14 |
| static SV *pc_resolve(pTHX_ SV *key, SV *arg, const char *where) { |
| 164 |
|
| STRLEN klen; |
| 165 |
14 |
| const char *k = SvPV_const(key, klen); |
| 166 |
|
| |
| 167 |
14 |
▶ | if (klen == 4 && memEQ(k, "$env", 4)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == 4 && memEQ(k, "$env", 4)) { |
6 |
8 |
14 |
| if (klen == 4 && memEQ(k, "$env", 4)) { |
6 |
0 |
6 |
|
| 168 |
|
| STRLEN nlen; |
| 169 |
6 |
| const char *name = SvPV_const(arg, nlen); |
| 170 |
6 |
| const char *val = getenv(name); |
| 171 |
6 |
▶ | if (!val) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!val) |
2 |
4 |
6 |
|
| 172 |
2 |
| croak("Punk::Config: %s wants the environment variable '%s', " |
| 173 |
|
| "which is not set", where, name); |
| 174 |
4 |
| return newSVpv(val, 0); |
| 175 |
|
| } |
| 176 |
8 |
▶ | if (klen == 5 && memEQ(k, "$file", 5)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == 5 && memEQ(k, "$file", 5)) { |
4 |
4 |
8 |
| if (klen == 5 && memEQ(k, "$file", 5)) { |
2 |
2 |
4 |
|
| 177 |
2 |
| const char *path = SvPV_nolen(arg); |
| 178 |
2 |
| SV *body = pc_slurp(aTHX_ path); |
| 179 |
|
| SV *out; |
| 180 |
2 |
▶ | if (!body) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!body) |
1 |
1 |
2 |
|
| 181 |
1 |
| croak("Punk::Config: %s cannot read '%s': %s", where, path, |
| 182 |
|
| Strerror(errno)); |
| 183 |
1 |
| out = newSVsv(body); |
| 184 |
1 |
| pc_chomp(aTHX_ out); |
| 185 |
1 |
| return out; |
| 186 |
|
| } |
| 187 |
6 |
▶ | if (klen == 5 && memEQ(k, "$exec", 5)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == 5 && memEQ(k, "$exec", 5)) { |
2 |
4 |
6 |
| if (klen == 5 && memEQ(k, "$exec", 5)) { |
1 |
1 |
2 |
|
| 188 |
|
| /* list form runs without a shell; a bare string is one command, |
| 189 |
|
| * which perl hands to the shell when it holds a metacharacter */ |
| 190 |
1 |
▶ | AV *av = (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| AV *av = (SvROK(arg) && SvTYPE(SvRV(arg)) == SVt_PVAV) |
1 |
0 |
1 |
|
| 191 |
2 |
▶ | ? (AV *)SvRV(arg) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (AV *)SvRV(arg) : NULL; |
1 |
0 |
1 |
|
| 192 |
|
| SV *out; |
| 193 |
1 |
| dSP; int count; |
| 194 |
1 |
| ENTER; SAVETMPS; |
| 195 |
1 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
1 |
1 |
|
| 196 |
1 |
▶ | if (av) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av) { |
1 |
0 |
1 |
|
| 197 |
1 |
| SSize_t i, n = av_len(av) + 1; |
| 198 |
1 |
▶ | if (!n) croak("Punk::Config: %s has an empty $exec", where); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) croak("Punk::Config: %s has an empty $exec", where); |
0 |
1 |
1 |
|
| 199 |
1 |
▶ | EXTEND(SP, n); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, n); |
1 |
0 |
1 |
| EXTEND(SP, n); |
0 |
1 |
1 |
|
| 200 |
3 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
2 |
1 |
3 |
|
| 201 |
2 |
| SV **e = av_fetch(av, i, 0); |
| 202 |
2 |
▶ | PUSHs(e && *e ? *e : &PL_sv_undef); |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHs(e && *e ? *e : &PL_sv_undef); |
2 |
0 |
2 |
| PUSHs(e && *e ? *e : &PL_sv_undef); |
2 |
0 |
2 |
|
| 203 |
|
| } |
| 204 |
|
| } |
| 205 |
0 |
▶ | else XPUSHs(arg); |
Branch: 1 branch
| Branch | True | False | Total |
| else XPUSHs(arg); |
0 |
0 |
0 |
|
| 206 |
1 |
| PUTBACK; |
| 207 |
1 |
| count = call_pv("Punk::Config::_exec", G_SCALAR | G_EVAL); |
| 208 |
1 |
| SPAGAIN; |
| 209 |
1 |
▶ | if (SvTRUE(ERRSV)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) { |
1 |
0 |
1 |
| if (SvTRUE(ERRSV)) { |
0 |
1 |
1 |
|
| 210 |
0 |
▶ | SV *e = newSVsv(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| SV *e = newSVsv(ERRSV); |
0 |
0 |
0 |
|
| 211 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 212 |
0 |
| croak("Punk::Config: %s - %s", where, |
| 213 |
|
| SvPV_nolen(sv_2mortal(e))); |
| 214 |
|
| } |
| 215 |
1 |
▶ | out = count > 0 ? newSVsv(POPs) : newSVpvs(""); |
Branch: 1 branch
| Branch | True | False | Total |
| out = count > 0 ? newSVsv(POPs) : newSVpvs(""); |
1 |
0 |
1 |
|
| 216 |
1 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
1 |
0 |
1 |
|
| 217 |
1 |
| pc_chomp(aTHX_ out); |
| 218 |
1 |
| return out; |
| 219 |
|
| } |
| 220 |
5 |
▶ | if (klen == 8 && memEQ(k, "$literal", 8)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (klen == 8 && memEQ(k, "$literal", 8)) |
4 |
1 |
5 |
| if (klen == 8 && memEQ(k, "$literal", 8)) |
4 |
0 |
4 |
|
| 221 |
4 |
| return newSVsv(arg); /* deliberate plaintext, spelled out */ |
| 222 |
|
| |
| 223 |
1 |
| croak("Punk::Config: %s uses an unknown resolver '%s' " |
| 224 |
|
| "(known: $env, $exec, $file, $literal)", where, k); |
| 225 |
|
| return NULL; /* not reached */ |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
|
| /* ---- the walk ------------------------------------------------------------ |
| 229 |
|
| * One pass: resolve references into `resolved` and `secrets`, and build the |
| 230 |
|
| * public copy with each secret replaced by [redacted]. Returns the public |
| 231 |
|
| * node (owned); *resolved_out gets the resolved node (owned). */ |
| 232 |
361 |
| static SV *pc_walk(pTHX_ punk_config *cfg, SV *node, SV *path, |
| 233 |
|
| SV **resolved_out, int depth) { |
| 234 |
361 |
| SV *arg = NULL; |
| 235 |
|
| SV *key; |
| 236 |
361 |
▶ | if (depth > PC_MAX_DEPTH) |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth > PC_MAX_DEPTH) |
1 |
360 |
361 |
|
| 237 |
1 |
| croak("Punk::Config: nested deeper than %d levels (an alias " |
| 238 |
|
| "cycle?)", PC_MAX_DEPTH); |
| 239 |
360 |
| key = pc_reference(aTHX_ node, &arg); |
| 240 |
|
| |
| 241 |
360 |
▶ | if (key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key) { |
14 |
346 |
360 |
|
| 242 |
28 |
▶ | const char *where_path = SvCUR(path) ? SvPV_nolen(path) : "(root)"; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *where_path = SvCUR(path) ? SvPV_nolen(path) : "(root)"; |
14 |
0 |
14 |
|
| 243 |
14 |
| SV *where = sv_2mortal(newSVpvf("'%s'", where_path)); |
| 244 |
14 |
| SV *value = pc_resolve(aTHX_ key, arg, SvPV_nolen(where)); |
| 245 |
10 |
| (void)hv_store_ent(cfg->secrets, path, newSVsv(value), 0); |
| 246 |
10 |
| *resolved_out = value; /* the real thing */ |
| 247 |
10 |
| return newSVpvs("[redacted]"); /* what the public copy sees */ |
| 248 |
|
| } |
| 249 |
|
| |
| 250 |
346 |
▶ | if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { |
346 |
0 |
346 |
| if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { |
236 |
110 |
346 |
| if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { |
225 |
11 |
236 |
|
| 251 |
225 |
| HV *src = (HV *)SvRV(node); |
| 252 |
225 |
| HV *pub = newHV(), *res = newHV(); |
| 253 |
|
| HE *he; |
| 254 |
225 |
| hv_iterinit(src); |
| 255 |
450 |
▶ | while ((he = hv_iternext(src))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(src))) { |
295 |
155 |
450 |
|
| 256 |
295 |
▶ | SV *k = HeSVKEY_force(he); |
Branch: 2 branches
| Branch | True | False | Total |
| SV *k = HeSVKEY_force(he); |
295 |
0 |
295 |
| SV *k = HeSVKEY_force(he); |
0 |
295 |
295 |
|
| 257 |
|
| STRLEN klen; |
| 258 |
295 |
| const char *kp = SvPV_const(k, klen); |
| 259 |
590 |
| SV *child = SvCUR(path) |
| 260 |
202 |
| ? sv_2mortal(newSVpvf("%s.%s", SvPV_nolen(path), kp)) |
| 261 |
295 |
▶ | : sv_2mortal(newSVsv(k)); |
Branch: 1 branch
| Branch | True | False | Total |
| : sv_2mortal(newSVsv(k)); |
202 |
93 |
295 |
|
| 262 |
295 |
| SV *rnode = NULL; |
| 263 |
|
| SV *pnode; |
| 264 |
295 |
| pnode = pc_walk(aTHX_ cfg, HeVAL(he), child, &rnode, depth + 1); |
| 265 |
225 |
| (void)hv_store_ent(pub, k, pnode, 0); |
| 266 |
225 |
▶ | (void)hv_store_ent(res, k, rnode ? rnode : newSVsv(HeVAL(he)), 0); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_store_ent(res, k, rnode ? rnode : newSVsv(HeVAL(he)), 0); |
0 |
225 |
225 |
|
| 267 |
|
| } |
| 268 |
155 |
| *resolved_out = newRV_noinc((SV *)res); |
| 269 |
155 |
| return newRV_noinc((SV *)pub); |
| 270 |
|
| } |
| 271 |
|
| |
| 272 |
121 |
▶ | if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { |
121 |
0 |
121 |
| if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { |
11 |
110 |
121 |
| if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { |
5 |
6 |
11 |
|
| 273 |
5 |
| AV *src = (AV *)SvRV(node); |
| 274 |
5 |
| AV *pub = newAV(), *res = newAV(); |
| 275 |
5 |
| SSize_t i, n = av_len(src) + 1; |
| 276 |
14 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
9 |
5 |
14 |
|
| 277 |
9 |
| SV **e = av_fetch(src, i, 0); |
| 278 |
9 |
| SV *child = sv_2mortal(newSVpvf("%s[%" IVdf "]", |
| 279 |
|
| SvPV_nolen(path), (IV)i)); |
| 280 |
9 |
| SV *rnode = NULL; |
| 281 |
9 |
▶ | SV *pnode = pc_walk(aTHX_ cfg, e ? *e : &PL_sv_undef, child, |
Branch: 1 branch
| Branch | True | False | Total |
| SV *pnode = pc_walk(aTHX_ cfg, e ? *e : &PL_sv_undef, child, |
9 |
0 |
9 |
|
| 282 |
|
| &rnode, depth + 1); |
| 283 |
9 |
| av_push(pub, pnode); |
| 284 |
9 |
▶ | av_push(res, rnode ? rnode : newSVsv(e ? *e : &PL_sv_undef)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(res, rnode ? rnode : newSVsv(e ? *e : &PL_sv_undef)); |
0 |
9 |
9 |
| av_push(res, rnode ? rnode : newSVsv(e ? *e : &PL_sv_undef)); |
0 |
0 |
0 |
|
| 285 |
|
| } |
| 286 |
5 |
| *resolved_out = newRV_noinc((SV *)res); |
| 287 |
5 |
| return newRV_noinc((SV *)pub); |
| 288 |
|
| } |
| 289 |
|
| |
| 290 |
116 |
| *resolved_out = newSVsv(node); |
| 291 |
116 |
| return newSVsv(node); |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
|
| /* ---- dotted-path lookup --------------------------------------------------- */ |
| 295 |
|
| |
| 296 |
8 |
| static SV *pc_at_path(pTHX_ SV *root, const char *path, STRLEN plen) { |
| 297 |
8 |
| SV *node = root; |
| 298 |
8 |
| STRLEN start = 0, i; |
| 299 |
100 |
▶ | for (i = 0; i <= plen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i <= plen; i++) { |
92 |
8 |
100 |
|
| 300 |
92 |
▶ | if (i == plen || path[i] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i == plen || path[i] == '.') { |
84 |
8 |
92 |
| if (i == plen || path[i] == '.') { |
7 |
77 |
84 |
|
| 301 |
15 |
▶ | if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) |
15 |
0 |
15 |
| if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) |
15 |
0 |
15 |
| if (!(node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV)) |
0 |
15 |
15 |
|
| 302 |
0 |
| return NULL; |
| 303 |
|
| { |
| 304 |
15 |
| HE *he = hv_fetch_ent((HV *)SvRV(node), |
| 305 |
|
| sv_2mortal(newSVpvn(path + start, i - start)), 0, 0); |
| 306 |
15 |
▶ | if (!he) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!he) return NULL; |
0 |
15 |
15 |
|
| 307 |
15 |
| node = HeVAL(he); |
| 308 |
|
| } |
| 309 |
15 |
| start = i + 1; |
| 310 |
|
| } |
| 311 |
|
| } |
| 312 |
8 |
| return node; |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
53 |
| static void pc_free(pTHX_ punk_config *cfg) { |
| 316 |
53 |
▶ | if (!cfg) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cfg) return; |
0 |
53 |
53 |
|
| 317 |
53 |
▶ | if (cfg->public) SvREFCNT_dec((SV *)cfg->public); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->public) SvREFCNT_dec((SV *)cfg->public); |
52 |
1 |
53 |
|
| 318 |
53 |
▶ | if (cfg->resolved) SvREFCNT_dec((SV *)cfg->resolved); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->resolved) SvREFCNT_dec((SV *)cfg->resolved); |
52 |
1 |
53 |
|
| 319 |
53 |
▶ | if (cfg->secrets) SvREFCNT_dec((SV *)cfg->secrets); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->secrets) SvREFCNT_dec((SV *)cfg->secrets); |
53 |
0 |
53 |
|
| 320 |
53 |
▶ | if (cfg->files) SvREFCNT_dec((SV *)cfg->files); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->files) SvREFCNT_dec((SV *)cfg->files); |
53 |
0 |
53 |
|
| 321 |
53 |
▶ | if (cfg->file) SvREFCNT_dec(cfg->file); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->file) SvREFCNT_dec(cfg->file); |
53 |
0 |
53 |
|
| 322 |
53 |
▶ | if (cfg->env) SvREFCNT_dec(cfg->env); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cfg->env) SvREFCNT_dec(cfg->env); |
53 |
0 |
53 |
|
| 323 |
53 |
| Safefree(cfg); |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
|
| #endif /* PUNK_CONFIG_H */ |