xs/config.xs

filter: Statementstmt 94.1% 81 / 86 Branchbran 67.0% 59 / 88 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.4% 140 / 174 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_reqid.h ↑ Summary include/punk/punk_mrows.h »
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 59 SV *file = NULL, *env = NULL;
17 59 SV *merged = NULL;
18 const char *stem, *ext;
19 STRLEN flen;
20 char layer[2048];
21 int i;
22  
23 142 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 84 58 142
24 STRLEN kl;
25 84 const char *k = SvPV_const(ST(i), kl);
26 84 ▶ if (kl == 4 && memEQ(k, "file", 4)) file = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 4 && memEQ(k, "file", 4)) file = ST(i + 1); 59 25 84
if (kl == 4 && memEQ(k, "file", 4)) file = ST(i + 1); 59 0 59
27 25 ▶ else if (kl == 3 && memEQ(k, "env", 3)) env = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 3 && memEQ(k, "env", 3)) env = ST(i + 1); 24 1 25
else if (kl == 3 && memEQ(k, "env", 3)) env = ST(i + 1); 24 0 24
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 1 ▶ else if (kl == 7 && memEQ(k, "secrets", 7))
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 7 && memEQ(k, "secrets", 7)) 1 0 1
else if (kl == 7 && memEQ(k, "secrets", 7)) 1 0 1
33 1 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 0 else croak("Punk::Config->load: unknown option '%.*s'",
38 (int)kl, k);
39 }
40 58 ▶ if (!(file && SvOK(file)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(file && SvOK(file))) 58 0 58
if (!(file && SvOK(file))) 0 58 58
41 0 croak("Punk::Config->load: a file is required");
42  
43 58 Newxz(cfg, 1, punk_config);
44 58 cfg->file = newSVsv(file);
45 58 cfg->secrets = newHV();
46 58 cfg->files = newAV();
47 82 ▶ cfg->env = (env && SvOK(env)) ? newSVsv(env)
Branch: 1 branch
BranchTrueFalseTotal
cfg->env = (env && SvOK(env)) ? newSVsv(env) 24 0 24
48 58 ▶ : newSVpv(getenv("PUNK_ENV") ? getenv("PUNK_ENV")
Branch: 2 branches
BranchTrueFalseTotal
: newSVpv(getenv("PUNK_ENV") ? getenv("PUNK_ENV") 24 34 58
: 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 58 const char *f = SvPV_const(file, flen);
54 58 const char *dot = NULL;
55 STRLEN j;
56 232 ▶ for (j = flen; j > 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = flen; j > 0; j--) { 232 0 232
57 232 ▶ if (f[j - 1] == '/') break;
Branch: 1 branch
BranchTrueFalseTotal
if (f[j - 1] == '/') break; 0 232 232
58 232 ▶ if (f[j - 1] == '.') { dot = f + j - 1; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (f[j - 1] == '.') { dot = f + j - 1; break; } 58 174 232
59 }
60 58 stem = f;
61 58 ▶ ext = dot ? dot : "";
Branch: 1 branch
BranchTrueFalseTotal
ext = dot ? dot : ""; 58 0 58
62 232 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 174 58 232
63 SV *body, *doc;
64 174 ▶ STRLEN stem_len = dot ? (STRLEN)(dot - f) : flen;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN stem_len = dot ? (STRLEN)(dot - f) : flen; 174 0 174
65 174 ▶ if (i == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (i == 0) 58 116 174
66 58 my_snprintf(layer, sizeof layer, "%.*s%s",
67 (int)stem_len, stem, ext);
68 else
69 174 ▶ my_snprintf(layer, sizeof layer, "%.*s.%s%s",
Branch: 1 branch
BranchTrueFalseTotal
my_snprintf(layer, sizeof layer, "%.*s.%s%s", 58 58 116
70 (int)stem_len, stem,
71 116 i == 1 ? SvPV_nolen(cfg->env) : "local", ext);
72 174 body = pc_slurp(aTHX_ layer);
73 174 ▶ if (!body) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!body) continue; 109 65 174
74 65 av_push(cfg->files, newSVpv(layer, 0));
75 65 doc = pc_yaml_decode(aTHX_ body, layer);
76 65 ▶ if (!(SvOK(doc) && SvROK(doc)
Branch: 2 branches
BranchTrueFalseTotal
if (!(SvOK(doc) && SvROK(doc) 65 0 65
if (!(SvOK(doc) && SvROK(doc) 65 0 65
77 65 ▶ && SvTYPE(SvRV(doc)) == SVt_PVHV)) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(doc)) == SVt_PVHV)) { 0 65 65
78 0 pc_free(aTHX_ cfg);
79 0 croak("Punk::Config: '%s' is not a YAML mapping", layer);
80 }
81 65 merged = merged ? sv_2mortal(pc_merge(aTHX_ merged, doc, 0))
82 65 ▶ : sv_2mortal(newSVsv(doc));
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSVsv(doc)); 8 57 65
83 }
84 }
85 58 ▶ if (av_len(cfg->files) < 0) { /* -1: nothing was readable */
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(cfg->files) < 0) { /* -1: nothing was readable */ 1 57 58
86 1 SV *f = sv_2mortal(newSVsv(cfg->file));
87 1 pc_free(aTHX_ cfg);
88 1 croak("Punk::Config: no config file found (looked for '%s')",
89 SvPV_nolen(f));
90 }
91  
92 { /* resolve + redact in one walk */
93 57 SV *resolved = NULL;
94 57 SV *public = pc_walk(aTHX_ cfg, merged,
95 sv_2mortal(newSVpvs("")), &resolved, 0);
96 52 ▶ cfg->public = (SvROK(public) && SvTYPE(SvRV(public)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
cfg->public = (SvROK(public) && SvTYPE(SvRV(public)) == SVt_PVHV) 52 0 52
97 104 ▶ ? (HV *)SvREFCNT_inc(SvRV(public)) : newHV();
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvREFCNT_inc(SvRV(public)) : newHV(); 52 0 52
98 52 ▶ cfg->resolved = (resolved && SvROK(resolved)
Branch: 1 branch
BranchTrueFalseTotal
cfg->resolved = (resolved && SvROK(resolved) 52 0 52
99 52 ▶ && SvTYPE(SvRV(resolved)) == SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(resolved)) == SVt_PVHV) 52 0 52
100 104 ▶ ? (HV *)SvREFCNT_inc(SvRV(resolved)) : newHV();
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvREFCNT_inc(SvRV(resolved)) : newHV(); 52 0 52
101 52 SvREFCNT_dec(public);
102 52 ▶ if (resolved) SvREFCNT_dec(resolved);
Branch: 1 branch
BranchTrueFalseTotal
if (resolved) SvREFCNT_dec(resolved); 52 0 52
103 }
104  
105 104 RETVAL = sv_setref_iv(newSV(0), SvPV_nolen(class), PTR2IV(cfg));
106 }
107 OUTPUT:
108 RETVAL
109  
110 # The public structure: secrets replaced by [redacted].
111 SV *
112 config(self)
113 SV *self
114 ALIAS:
115 resolved = 1
116 CODE:
117 {
118 44 punk_config *cfg = punk_config_of(aTHX_ self);
119 44 ▶ HV *hv = ix ? cfg->resolved : cfg->public;
Branch: 1 branch
BranchTrueFalseTotal
HV *hv = ix ? cfg->resolved : cfg->public; 35 9 44
120 44 RETVAL = newRV_inc((SV *)hv);
121 }
122 OUTPUT:
123 RETVAL
124  
125 SV *
126 env(self)
127 SV *self
128 CODE:
129 50 RETVAL = newSVsv(punk_config_of(aTHX_ self)->env);
130 OUTPUT:
131 RETVAL
132  
133 SV *
134 files(self)
135 SV *self
136 CODE:
137 {
138 2 punk_config *cfg = punk_config_of(aTHX_ self);
139 2 AV *out = newAV();
140 2 SSize_t i, n = av_len(cfg->files) + 1;
141 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 4 2 6
142 4 SV **e = av_fetch(cfg->files, i, 0);
143 4 ▶ av_push(out, newSVsv(e && *e ? *e : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, newSVsv(e && *e ? *e : &PL_sv_undef)); 4 0 4
av_push(out, newSVsv(e && *e ? *e : &PL_sv_undef)); 4 0 4
144 }
145 2 RETVAL = newRV_noinc((SV *)out);
146 }
147 OUTPUT:
148 RETVAL
149  
150 # The resolved value at a dotted path - secrets included. What the boot
151 # consumers read.
152 SV *
153 get(self, path)
154 SV *self
155 SV *path
156 CODE:
157 {
158 8 punk_config *cfg = punk_config_of(aTHX_ self);
159 8 SV *root = sv_2mortal(newRV_inc((SV *)cfg->resolved));
160 STRLEN plen;
161 8 const char *p = SvPV_const(path, plen);
162 8 SV *found = pc_at_path(aTHX_ root, p, plen);
163 8 ▶ RETVAL = found ? newSVsv(found) : newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = found ? newSVsv(found) : newSV(0); 8 0 8
164 }
165 OUTPUT:
166 RETVAL
167  
168 SV *
169 secret(self, path)
170 SV *self
171 SV *path
172 CODE:
173 {
174 6 punk_config *cfg = punk_config_of(aTHX_ self);
175 6 HE *he = hv_fetch_ent(cfg->secrets, path, 0, 0);
176 6 ▶ if (!he)
Branch: 1 branch
BranchTrueFalseTotal
if (!he) 0 6 6
177 0 croak("Punk::Config: no secret at '%s'", SvPV_nolen(path));
178 6 RETVAL = newSVsv(HeVAL(he));
179 }
180 OUTPUT:
181 RETVAL
182  
183 int
184 has_secret(self, path)
185 SV *self
186 SV *path
187 CODE:
188 2 RETVAL = hv_exists_ent(punk_config_of(aTHX_ self)->secrets, path, 0)
189 2 ▶ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
? 1 : 0; 0 2 2
190 OUTPUT:
191 RETVAL
192  
193 # The dotted paths of every resolved secret, sorted.
194 SV *
195 secret_paths(self)
196 SV *self
197 CODE:
198 {
199 2 punk_config *cfg = punk_config_of(aTHX_ self);
200 2 AV *out = newAV();
201 HE *he;
202 2 hv_iterinit(cfg->secrets);
203 6 ▶ while ((he = hv_iternext(cfg->secrets)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(cfg->secrets))) 4 2 6
204 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
205 2 sortsv(AvARRAY(out), (U32)(av_len(out) + 1), Perl_sv_cmp);
206 2 RETVAL = newRV_noinc((SV *)out);
207 }
208 OUTPUT:
209 RETVAL
210  
211 void
212 DESTROY(self)
213 SV *self
214 CODE:
215 {
216 52 ▶ if (SvROK(self) && SvIOK(SvRV(self)))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self) && SvIOK(SvRV(self))) 52 0 52
if (SvROK(self) && SvIOK(SvRV(self))) 52 0 52
217 52 pc_free(aTHX_ punk_config_of(aTHX_ self));
218 }
« include/punk/punk_reqid.h ↑ Summary include/punk/punk_mrows.h »