xs/config.xs

filter: Statementstmt 97.6% 84 / 86 Branchbran 69.3% 63 / 88 (2 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.3% 147 / 174 (2 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_blob.h ↑ Summary xs/mrows.xs »
1 MODULE = Punk PACKAGE = Punk::Config
2  
3 PROTOTYPES: DISABLE
4  
5 # Punk::Config->load(file => $path, env => $name, secrets => $mode)
6 #
7 # Read punk.yml, then punk.$env.yml, then punk.local.yml; merge them;
8 # resolve every secret reference; build the redacted public copy. All of it
9 # here, except the YAML parse itself (one YAML::XS call per file).
10 SV *
11 load(class, ...)
12 SV *class
13 CODE:
14 {
15 punk_config *cfg;
16 70 SV *file = NULL, *env = NULL;
17 70 SV *merged = NULL;
18 const char *stem, *ext;
19 STRLEN flen;
20 char layer[2048];
21 int i;
22  
23 172 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 105 67 172
24 STRLEN kl;
25 105 const char *k = SvPV_const(ST(i), kl);
26 105 ▶ if (kl == 4 && memEQ(k, "file", 4)) file = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "file", 4)) file = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 69 36 105
if (kl == 4 && memEQ(k, "file", 4)) file = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 69 0 69
27 36 ▶ else if (kl == 3 && memEQ(k, "env", 3)) env = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 3 && memEQ(k, "env", 3)) env = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 33 3 36
else if (kl == 3 && memEQ(k, "env", 3)) env = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 33 0 33
28 /* Named rather than left to the generic message below, because
29 * this one used to work: whether a password sits in the file in
30 * plaintext is the person writing the file's decision, so there
31 * is no longer a mode to put it in. */
32 3 ▶ else if (kl == 7 && memEQ(k, "secrets", 7)) /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 7 && memEQ(k, "secrets", 7)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 1 3
else if (kl == 7 && memEQ(k, "secrets", 7)) /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
33 2 croak("Punk::Config->load: the 'secrets' option is gone - "
34 "a plaintext value in the file is no longer second-"
35 "guessed. Drop the option; secret(), $env, $file and "
36 "$exec are unchanged");
37 1 else croak("Punk::Config->load: unknown option '%.*s'",
38 (int)kl, k);
39 }
40 67 ▶ if (!(file && SvOK(file))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!(file && SvOK(file))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 66 1 67
if (!(file && SvOK(file))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 66 66
41 1 croak("Punk::Config->load: a file is required");
42  
43 66 Newxz(cfg, 1, punk_config);
44 66 cfg->file = newSVsv(file);
45 66 cfg->secrets = newHV();
46 66 cfg->files = newAV();
47 98 ▶ cfg->env = (env && SvOK(env)) ? newSVsv(env)
Branch: 1 branch
BranchTrueFalseTotal
cfg->env = (env && SvOK(env)) ? newSVsv(env) 32 0 32
48 66 ▶ : newSVpv(getenv("PUNK_ENV") ? getenv("PUNK_ENV")
Branch: 2 branches
BranchTrueFalseTotal
: newSVpv(getenv("PUNK_ENV") ? getenv("PUNK_ENV") 32 34 66
: newSVpv(getenv("PUNK_ENV") ? getenv("PUNK_ENV") 8 26 34
49 : "production", 0);
50  
51 /* config/punk.yml -> punk.yml, punk.<env>.yml, punk.local.yml */
52 {
53 66 const char *f = SvPV_const(file, flen);
54 66 const char *dot = NULL;
55 STRLEN j;
56 264 ▶ # uncoverable branch false
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch false 264 0 264
57 264 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 0 264 264
58 264 ▶ for (j = flen; j > 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = flen; j > 0; j--) { 66 198 264
59 # uncoverable branch true
60 66 # uncoverable condition left count:1 class:ignore_covered_err
61 66 ▶ if (f[j - 1] == '/') break;
Branch: 1 branch
BranchTrueFalseTotal
if (f[j - 1] == '/') break; 66 0 66
62 264 ▶ if (f[j - 1] == '.') { dot = f + j - 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (f[j - 1] == '.') { dot = f + j - 1; break; } 198 66 264
63 }
64 198 ▶ stem = f;
Branch: 1 branch
BranchTrueFalseTotal
stem = f; 198 0 198
65 198 ▶ ext = dot ? dot : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
ext = dot ? dot : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 66 132 198
66 66 for (i = 0; i < 3; i++) {
67 SV *body, *doc;
68 STRLEN stem_len = dot ? (STRLEN)(dot - f) : flen; /* # uncoverable condition right count:1 class:ignore_covered_err */
69 198 ▶ if (i == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) 66 66 132
70 my_snprintf(layer, sizeof layer, "%.*s%s",
71 132 (int)stem_len, stem, ext);
72 198 else
73 198 ▶ my_snprintf(layer, sizeof layer, "%.*s.%s%s",
Branch: 1 branch
BranchTrueFalseTotal
my_snprintf(layer, sizeof layer, "%.*s.%s%s", 123 75 198
74 75 (int)stem_len, stem,
75 75 i == 1 ? SvPV_nolen(cfg->env) : "local", ext);
76 75 ▶ body = pc_slurp(aTHX_ layer);
Branch: 2 branches
BranchTrueFalseTotal
body = pc_slurp(aTHX_ layer); 75 0 75
body = pc_slurp(aTHX_ layer); 75 0 75
77 75 ▶ if (!body) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!body) continue; 0 75 75
78 0 av_push(cfg->files, newSVpv(layer, 0));
79 0 doc = pc_yaml_decode(aTHX_ body, layer);
80 # uncoverable branch true
81 75 # uncoverable condition left count:1 class:ignore_covered_err
82 75 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 10 65 75
83 # uncoverable condition left count:3 class:ignore_covered_err
84 if (!(SvOK(doc) && SvROK(doc)
85 66 ▶ && SvTYPE(SvRV(doc)) == SVt_PVHV)) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(doc)) == SVt_PVHV)) { 1 65 66
86 1 pc_free(aTHX_ cfg); /* # uncoverable statement */
87 1 croak("Punk::Config: '%s' is not a YAML mapping", layer); /* # uncoverable statement */
88 1 }
89 merged = merged ? sv_2mortal(pc_merge(aTHX_ merged, doc, 0))
90 : sv_2mortal(newSVsv(doc));
91 }
92 }
93 65 if (av_len(cfg->files) < 0) { /* -1: nothing was readable */
94 65 SV *f = sv_2mortal(newSVsv(cfg->file));
95 pc_free(aTHX_ cfg);
96 57 ▶ croak("Punk::Config: no config file found (looked for '%s')",
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Config: no config file found (looked for '%s')", 57 0 57
97 114 ▶ SvPV_nolen(f));
Branch: 1 branch
BranchTrueFalseTotal
SvPV_nolen(f)); 57 0 57
98 57 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
} 57 0 57
99 57 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
57 0 57
100 114 ▶ { /* resolve + redact in one walk */
Branch: 1 branch
BranchTrueFalseTotal
{ /* resolve + redact in one walk */ 57 0 57
101 57 SV *resolved = NULL;
102 57 ▶ SV *public = pc_walk(aTHX_ cfg, merged,
Branch: 1 branch
BranchTrueFalseTotal
SV *public = pc_walk(aTHX_ cfg, merged, 57 0 57
103 sv_2mortal(newSVpvs("")), &resolved, 0);
104 cfg->public = (SvROK(public) && SvTYPE(SvRV(public)) == SVt_PVHV)
105 114 ? (HV *)SvREFCNT_inc(SvRV(public)) : newHV();
106 cfg->resolved = (resolved && SvROK(resolved)
107 && SvTYPE(SvRV(resolved)) == SVt_PVHV)
108 ? (HV *)SvREFCNT_inc(SvRV(resolved)) : newHV();
109 SvREFCNT_dec(public);
110 # uncoverable branch false class:ignore_covered_err
111 # uncoverable condition right count:1 class:ignore_covered_err
112 if (resolved) SvREFCNT_dec(resolved);
113 }
114  
115 RETVAL = sv_setref_iv(newSV(0), SvPV_nolen(class), PTR2IV(cfg));
116 }
117 OUTPUT:
118 RETVAL
119  
120 # The public structure: secrets replaced by [redacted].
121 SV *
122 config(self)
123 SV *self
124 ALIAS:
125 resolved = 1
126 CODE:
127 {
128 47 punk_config *cfg = punk_config_of(aTHX_ self);
129 47 ▶ HV *hv = ix ? cfg->resolved : cfg->public;
Branch: 1 branch
BranchTrueFalseTotal
HV *hv = ix ? cfg->resolved : cfg->public; 36 11 47
130 47 RETVAL = newRV_inc((SV *)hv);
131 }
132 OUTPUT:
133 RETVAL
134  
135 SV *
136 env(self)
137 SV *self
138 CODE:
139 51 RETVAL = newSVsv(punk_config_of(aTHX_ self)->env);
140 OUTPUT:
141 RETVAL
142  
143 SV *
144 files(self)
145 SV *self
146 CODE:
147 {
148 3 punk_config *cfg = punk_config_of(aTHX_ self);
149 3 AV *out = newAV();
150 3 SSize_t i, n = av_len(cfg->files) + 1;
151 9 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 6 3 9
152 6 SV **e = av_fetch(cfg->files, i, 0);
153 6 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 6 0 6
# uncoverable condition right count:1 class:ignore_covered_err 6 0 6
154 # uncoverable condition right count:2 class:ignore_covered_err
155 3 av_push(out, newSVsv(e && *e ? *e : &PL_sv_undef));
156 }
157 RETVAL = newRV_noinc((SV *)out);
158 }
159 OUTPUT:
160 RETVAL
161  
162 # The resolved value at a dotted path - secrets included. What the boot
163 # consumers read.
164 SV *
165 get(self, path)
166 SV *self
167 SV *path
168 CODE:
169 {
170 11 punk_config *cfg = punk_config_of(aTHX_ self);
171 11 SV *root = sv_2mortal(newRV_inc((SV *)cfg->resolved));
172 STRLEN plen;
173 11 const char *p = SvPV_const(path, plen);
174 11 SV *found = pc_at_path(aTHX_ root, p, plen);
175 11 ▶ RETVAL = found ? newSVsv(found) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = found ? newSVsv(found) : newSV(0); 9 2 11
176 }
177 OUTPUT:
178 RETVAL
179  
180 SV *
181 secret(self, path)
182 SV *self
183 SV *path
184 CODE:
185 {
186 9 punk_config *cfg = punk_config_of(aTHX_ self);
187 9 HE *he = hv_fetch_ent(cfg->secrets, path, 0, 0);
188 9 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 2 7 9
189 2 croak("Punk::Config: no secret at '%s'", SvPV_nolen(path));
190 7 RETVAL = newSVsv(HeVAL(he));
191 }
192 OUTPUT:
193 RETVAL
194  
195 int
196 has_secret(self, path)
197 SV *self
198 SV *path
199 CODE:
200 2 RETVAL = hv_exists_ent(punk_config_of(aTHX_ self)->secrets, path, 0)
201 2 ▶ ? 1 : 0; /* # uncoverable condition left count:1 */
Branch: 1 branch
BranchTrueFalseTotal
? 1 : 0; /* # uncoverable condition left count:1 */ 0 2 2
202 OUTPUT:
203 RETVAL
204  
205 # The dotted paths of every resolved secret, sorted.
206 SV *
207 secret_paths(self)
208 SV *self
209 CODE:
210 {
211 2 punk_config *cfg = punk_config_of(aTHX_ self);
212 2 AV *out = newAV();
213 HE *he;
214 2 hv_iterinit(cfg->secrets);
215 6 ▶ while ((he = hv_iternext(cfg->secrets)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(cfg->secrets))) 4 2 6
216 4 ▶ av_push(out, newSVsv(HeSVKEY_force(he)));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, newSVsv(HeSVKEY_force(he))); 4 0 4
av_push(out, newSVsv(HeSVKEY_force(he))); 0 4 4
217 2 sortsv(AvARRAY(out), (U32)(av_len(out) + 1), Perl_sv_cmp);
218 2 RETVAL = newRV_noinc((SV *)out);
219 }
220 OUTPUT:
221 RETVAL
222  
223 void
224 DESTROY(self)
225 SV *self
226 CODE:
227 {
228 57 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 57 0 57
# uncoverable branch false 57 0 57
229 57 # uncoverable condition right count:1 class:ignore_covered_err
230 # uncoverable condition right count:2 class:ignore_covered_err
231 if (SvROK(self) && SvIOK(SvRV(self)))
232 pc_free(aTHX_ punk_config_of(aTHX_ self));
233 }
« include/punk/punk_blob.h ↑ Summary xs/mrows.xs »