add: [ingester] Tarball support
parent
4e9cdc17a9
commit
7367a5382e
84
main.go
84
main.go
|
@ -1,7 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"archive/tar"
|
||||||
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"compress/gzip"
|
||||||
"crypto/dsa"
|
"crypto/dsa"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
|
@ -46,6 +49,7 @@ type (
|
||||||
certPath string
|
certPath string
|
||||||
format string
|
format string
|
||||||
recursive bool
|
recursive bool
|
||||||
|
tarball bool
|
||||||
}
|
}
|
||||||
|
|
||||||
bigNumber big.Int
|
bigNumber big.Int
|
||||||
|
@ -81,6 +85,7 @@ var (
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
confdir = flag.String("c", "conf.sample", "configuration directory")
|
confdir = flag.String("c", "conf.sample", "configuration directory")
|
||||||
recursive = flag.Bool("r", false, "should it open the directory recursively")
|
recursive = flag.Bool("r", false, "should it open the directory recursively")
|
||||||
|
tarball = flag.Bool("t", false, "is it a tar archive")
|
||||||
format = flag.String("f", "json", "certificate file format [json, crt, der]")
|
format = flag.String("f", "json", "certificate file format [json, crt, der]")
|
||||||
pull = flag.Bool("p", true, "pull from redis?")
|
pull = flag.Bool("p", true, "pull from redis?")
|
||||||
cr redis.Conn
|
cr redis.Conn
|
||||||
|
@ -153,6 +158,7 @@ func main() {
|
||||||
// Parse Certificate Folder
|
// Parse Certificate Folder
|
||||||
c.certPath = string(readConfFile(*confdir, "certfolder"))
|
c.certPath = string(readConfFile(*confdir, "certfolder"))
|
||||||
c.recursive = *recursive
|
c.recursive = *recursive
|
||||||
|
c.tarball = *tarball
|
||||||
c.format = *format
|
c.format = *format
|
||||||
|
|
||||||
// DB
|
// DB
|
||||||
|
@ -203,13 +209,30 @@ func main() {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !info.IsDir() {
|
if !info.IsDir() {
|
||||||
processFile(c.certPath, path, c.format)
|
fd, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
bf := bufio.NewReader(fd)
|
||||||
|
processFile(bf, path, c.format)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
} else if c.tarball {
|
||||||
|
fd, err := os.Stat(c.certPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
switch mode := fd.Mode(); {
|
||||||
|
case mode.IsDir():
|
||||||
|
log.Fatal("With -t=true flag, you need to input a tarball")
|
||||||
|
case mode.IsRegular():
|
||||||
|
processTar(c.certPath, jsonPath, c.format)
|
||||||
|
break
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var path []string
|
var path []string
|
||||||
path = append(path, "./sessions")
|
path = append(path, "./sessions")
|
||||||
|
@ -221,7 +244,12 @@ func main() {
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
path[1] = f.Name()
|
path[1] = f.Name()
|
||||||
jsonPath = strings.Join(path, "/")
|
jsonPath = strings.Join(path, "/")
|
||||||
processFile(c.certPath, jsonPath, c.format)
|
fd, err := os.Open(jsonPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
bf := bufio.NewReader(fd)
|
||||||
|
processFile(bf, jsonPath, c.format)
|
||||||
|
|
||||||
// Exit Signal Handle
|
// Exit Signal Handle
|
||||||
select {
|
select {
|
||||||
|
@ -235,23 +263,57 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func processFile(fp string, p string, f string) {
|
func processTar(fp string, p string, f string) error {
|
||||||
|
fd, err := os.Open(fp)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
bf := bufio.NewReader(fd)
|
||||||
|
gzr, err := gzip.NewReader(bf)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer gzr.Close()
|
||||||
|
tr := tar.NewReader(gzr)
|
||||||
|
for {
|
||||||
|
header, err := tr.Next()
|
||||||
|
switch {
|
||||||
|
// if no more files are found return
|
||||||
|
case err == io.EOF:
|
||||||
|
return nil
|
||||||
|
// return any other error
|
||||||
|
case err != nil:
|
||||||
|
return err
|
||||||
|
// if the header is nil, just skip it (not sure how this happens)
|
||||||
|
case header == nil:
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
switch header.Typeflag {
|
||||||
|
case tar.TypeDir:
|
||||||
|
continue
|
||||||
|
case tar.TypeReg:
|
||||||
|
processFile(tr, fp, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func processFile(r io.Reader, fp string, f string) {
|
||||||
switch f {
|
switch f {
|
||||||
case "json":
|
case "json":
|
||||||
processJSON(fp, p)
|
processJSON(r, fp)
|
||||||
break
|
break
|
||||||
case "der":
|
case "der":
|
||||||
processDER(fp, p)
|
processDER(r, fp)
|
||||||
break
|
break
|
||||||
case "crt":
|
case "crt":
|
||||||
processCRT(fp, p)
|
processCRT(r, fp)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func processDER(fp string, p string) bool {
|
func processDER(r io.Reader, p string) bool {
|
||||||
// read corresponding der file
|
// read corresponding der file
|
||||||
dat, err := ioutil.ReadFile(p)
|
dat, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -316,14 +378,14 @@ J:
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func processCRT(fp string, p string) bool {
|
func processCRT(r io.Reader, fp string) bool {
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func processJSON(fp string, p string) bool {
|
func processJSON(r io.Reader, fp string) bool {
|
||||||
// read corresponding json file
|
// read corresponding json file
|
||||||
dat, err := ioutil.ReadFile(p)
|
dat, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue