Skip to content

Commit 7367a53

Browse files
committed
add: [ingester] Tarball support
1 parent 4e9cdc1 commit 7367a53

File tree

1 file changed

+73
-11
lines changed

1 file changed

+73
-11
lines changed

main.go

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package main
22

33
import (
4+
"archive/tar"
5+
"bufio"
46
"bytes"
7+
"compress/gzip"
58
"crypto/dsa"
69
"crypto/ecdsa"
710
"crypto/rsa"
@@ -46,6 +49,7 @@ type (
4649
certPath string
4750
format string
4851
recursive bool
52+
tarball bool
4953
}
5054

5155
bigNumber big.Int
@@ -81,6 +85,7 @@ var (
8185
db *sql.DB
8286
confdir = flag.String("c", "conf.sample", "configuration directory")
8387
recursive = flag.Bool("r", false, "should it open the directory recursively")
88+
tarball = flag.Bool("t", false, "is it a tar archive")
8489
format = flag.String("f", "json", "certificate file format [json, crt, der]")
8590
pull = flag.Bool("p", true, "pull from redis?")
8691
cr redis.Conn
@@ -153,6 +158,7 @@ func main() {
153158
// Parse Certificate Folder
154159
c.certPath = string(readConfFile(*confdir, "certfolder"))
155160
c.recursive = *recursive
161+
c.tarball = *tarball
156162
c.format = *format
157163

158164
// DB
@@ -203,13 +209,30 @@ func main() {
203209
return err
204210
}
205211
if !info.IsDir() {
206-
processFile(c.certPath, path, c.format)
212+
fd, err := os.Open(path)
213+
if err != nil {
214+
log.Fatal(err)
215+
}
216+
bf := bufio.NewReader(fd)
217+
processFile(bf, path, c.format)
207218
}
208219
return nil
209220
})
210221
if err != nil {
211222
log.Println(err)
212223
}
224+
} else if c.tarball {
225+
fd, err := os.Stat(c.certPath)
226+
if err != nil {
227+
log.Fatal(err)
228+
}
229+
switch mode := fd.Mode(); {
230+
case mode.IsDir():
231+
log.Fatal("With -t=true flag, you need to input a tarball")
232+
case mode.IsRegular():
233+
processTar(c.certPath, jsonPath, c.format)
234+
break
235+
}
213236
} else {
214237
var path []string
215238
path = append(path, "./sessions")
@@ -221,7 +244,12 @@ func main() {
221244
for _, f := range files {
222245
path[1] = f.Name()
223246
jsonPath = strings.Join(path, "/")
224-
processFile(c.certPath, jsonPath, c.format)
247+
fd, err := os.Open(jsonPath)
248+
if err != nil {
249+
log.Fatal(err)
250+
}
251+
bf := bufio.NewReader(fd)
252+
processFile(bf, jsonPath, c.format)
225253

226254
// Exit Signal Handle
227255
select {
@@ -235,23 +263,57 @@ func main() {
235263
}
236264
}
237265

238-
func processFile(fp string, p string, f string) {
266+
func processTar(fp string, p string, f string) error {
267+
fd, err := os.Open(fp)
268+
if err != nil {
269+
log.Fatal(err)
270+
}
271+
bf := bufio.NewReader(fd)
272+
gzr, err := gzip.NewReader(bf)
273+
if err != nil {
274+
return err
275+
}
276+
defer gzr.Close()
277+
tr := tar.NewReader(gzr)
278+
for {
279+
header, err := tr.Next()
280+
switch {
281+
// if no more files are found return
282+
case err == io.EOF:
283+
return nil
284+
// return any other error
285+
case err != nil:
286+
return err
287+
// if the header is nil, just skip it (not sure how this happens)
288+
case header == nil:
289+
continue
290+
}
291+
switch header.Typeflag {
292+
case tar.TypeDir:
293+
continue
294+
case tar.TypeReg:
295+
processFile(tr, fp, f)
296+
}
297+
}
298+
}
299+
300+
func processFile(r io.Reader, fp string, f string) {
239301
switch f {
240302
case "json":
241-
processJSON(fp, p)
303+
processJSON(r, fp)
242304
break
243305
case "der":
244-
processDER(fp, p)
306+
processDER(r, fp)
245307
break
246308
case "crt":
247-
processCRT(fp, p)
309+
processCRT(r, fp)
248310
break
249311
}
250312
}
251313

252-
func processDER(fp string, p string) bool {
314+
func processDER(r io.Reader, p string) bool {
253315
// read corresponding der file
254-
dat, err := ioutil.ReadFile(p)
316+
dat, err := ioutil.ReadAll(r)
255317
if err != nil {
256318
log.Fatal(err)
257319
}
@@ -316,14 +378,14 @@ J:
316378
return nil
317379
}
318380

319-
func processCRT(fp string, p string) bool {
381+
func processCRT(r io.Reader, fp string) bool {
320382

321383
return true
322384
}
323385

324-
func processJSON(fp string, p string) bool {
386+
func processJSON(r io.Reader, fp string) bool {
325387
// read corresponding json file
326-
dat, err := ioutil.ReadFile(p)
388+
dat, err := ioutil.ReadAll(r)
327389
if err != nil {
328390
log.Fatal(err)
329391
}

0 commit comments

Comments
 (0)