-
Notifications
You must be signed in to change notification settings - Fork 103
/
subdomains.go
129 lines (111 loc) · 4.29 KB
/
subdomains.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
Copyright 2018 Assetnote
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package subdomains
import (
"github.com/urfave/cli"
"github.com/assetnote/commonspeak2/assets"
"github.com/assetnote/commonspeak2/log"
"golang.org/x/net/context"
"github.com/valyala/fasttemplate"
"google.golang.org/api/option"
"cloud.google.com/go/bigquery"
"os"
"strings"
)
func CmdStatus(c *cli.Context) error {
verboseOpt := c.GlobalBool("verbose")
silentOpt := c.GlobalBool("silent")
project := c.GlobalString("project")
credentials := c.GlobalString("credentials")
limitArg := c.String("limit")
sources := c.String("sources")
outputFile := c.String("output")
if _, err := os.Stat(credentials); os.IsNotExist(err) {
log.Fatalln(`credentials file not found, either have credentials.json saved in this folder, or provide the
the credentials file path via the --credentials parameter.`)
}
if credentials == "" {
log.Fatalln("credentials are required, provide it using the --credentials parameter.")
}
if project == "" {
log.Fatalln("project is required, provide it using the --project parameter.")
}
fields := log.Fields{
"Mode": "Subdomains",
}
// Store generated templates in a string slice, if no
// source parameter is provided, the default is to
// run all sources available in the below switch statement
sqlTemplateStrings := make(map[string]string)
sourceList := strings.Split(sources, ",")
for _, s := range sourceList {
switch s {
case "hackernews":
hnSubdomainsSqlAsset, err := assets.Asset("data/sql/hackernews/subdomains.sql")
if err != nil {
log.Debug("SQL for HackerNews not found.")
}
hnSQLString := string(hnSubdomainsSqlAsset[:])
hnTemplate := fasttemplate.New(hnSQLString, "{{", "}}")
// TODO: Complete feature to extract data from the runs tables
// httpArchiveTablePrefix := generateTablePrefixForHA()
hnCompiledSql := hnTemplate.ExecuteString(map[string]interface{}{
"limit": limitArg,
})
if verboseOpt {
log.WithFields(fields).Infof("Compiled SQL Template: %s", hnCompiledSql)
}
sqlTemplateStrings["hackernews"] = hnCompiledSql
log.WithFields(fields).Info("Generated SQL template for HackerNews.")
case "httparchive":
haSubdomainsSqlAsset, err := assets.Asset("data/sql/http-archive/subdomains.sql")
if err != nil {
log.Debug("SQL for HTTPArchive not found.")
}
haSQLString := string(haSubdomainsSqlAsset[:])
haTemplate := fasttemplate.New(haSQLString, "{{", "}}")
// TODO: Complete feature to extract data from the runs tables
// httpArchiveTablePrefix := generateTablePrefixForHA()
haCompiledSql := haTemplate.ExecuteString(map[string]interface{}{
"limit": limitArg,
})
if verboseOpt {
log.WithFields(fields).Infof("Compiled SQL Template: %s", haCompiledSql)
}
sqlTemplateStrings["httparchive"] = haCompiledSql
log.WithFields(fields).Info("Generated SQL template for HTTPArchive.")
}
}
// Initialise BigQuery Golang Client
ctx := context.Background()
client, err := bigquery.NewClient(ctx, project, option.WithCredentialsFile(credentials))
if err != nil {
log.WithFields(fields).Warning("Failed to create a BigQuery client. Please check your credentials.")
}
// Iterate over generated templates and obtain results
for source, compiledSqlValue := range sqlTemplateStrings {
fields["Source"] = source
log.WithFields(fields).Info("Executing BigQuery SQL... this could take some time.")
rows, err := query(client, ctx, compiledSqlValue)
if err != nil {
fields["Error"] = err.Error()
log.WithFields(fields).Fatal("Error executing BigQuery SQL.")
}
resultsErr := handleResults(os.Stdout, rows, outputFile, source, silentOpt, verboseOpt)
if resultsErr != nil {
fields["Error"] = resultsErr.Error()
log.WithFields(fields).Fatal("Error handling results.")
}
}
return nil
}