Skip to content

Commit

Permalink
Add back sample queries; s3 request payment is now json; unroll calls…
Browse files Browse the repository at this point in the history
… to run in the launcher app
  • Loading branch information
gsoltis committed Feb 17, 2021
1 parent 4ec6c21 commit b86838f
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 5 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
.vscode/
__pycache__/
*.pyc
/sample_queries
/requirements.txt
/pg_data
/gf
Expand Down
2 changes: 1 addition & 1 deletion introspector/aws/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _import_bucket(proxy: ServiceProxy, bucket_metadata) -> Dict:
result[key] = attr_result[key]
else:
result[key] = attr_result
if key in ('Policy', ):
if key in ('Policy', 'RequestPayment'):
result[key] = json.loads(result[key])
elif op_name == 'get_bucket_location':
result['Location'] = {'LocationConstraint': 'us-east-1'}
Expand Down
2 changes: 1 addition & 1 deletion introspector/queries/0023-aws_s3_bucket.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SELECT
policy.attr_value::jsonb AS policy,
policystatus.attr_value::jsonb AS policystatus,
replication.attr_value::jsonb AS replication,
requestpayment.attr_value #>> '{}' AS requestpayment,
requestpayment.attr_value::jsonb AS requestpayment,
tagging.attr_value::jsonb AS tagging,
versioning.attr_value::jsonb AS versioning,
website.attr_value::jsonb AS website,
Expand Down
64 changes: 63 additions & 1 deletion launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/pkg/errors"
)

func requireIntrospectorComposition(ctx context.Context, cli *client.Client) types.Container {
Expand Down Expand Up @@ -65,8 +69,60 @@ func needsGcpCredential(userCmd []string) bool {
return false
}

func runFileCommand(filename string, rest []string) ([]string, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
cmd := []string{"run", string(bytes)}
cmd = append(cmd, rest...)
return cmd, nil
}

func unrollRunCommands(cmd []string) ([][]string, error) {
if len(cmd) < 2 || cmd[0] != "run" {
return [][]string{cmd}, nil
}
queryTarget := cmd[1]
info, err := os.Stat(queryTarget)
if os.IsNotExist(err) {
// Let introspector handle whatever
return [][]string{cmd}, nil
} else if err != nil {
return nil, errors.Wrapf(err, "Failed to stat %v", queryTarget)
}
if info.IsDir() {
infos, err := ioutil.ReadDir(queryTarget)
if err != nil {
return nil, errors.Wrapf(err, "Failed to ReadDir(%v)", queryTarget)
}
cmds := [][]string{}
for _, info := range infos {
if strings.HasSuffix(info.Name(), ".sql") {
filename := filepath.Join(queryTarget, info.Name())
subCommand, err := runFileCommand(filename, cmd[2:])
if err != nil {
return nil, err
}
cmds = append(cmds, subCommand)
}
}
return cmds, nil
}
runCommand, err := runFileCommand(queryTarget, cmd[2:])
if err != nil {
return nil, err
}
return [][]string{runCommand}, nil
}

func cmdPassthrough(ctx context.Context, cli *client.Client, introspector types.Container, userCmd []string) error {
var env map[string]string

cmd := append([]string{"python", "introspector.py"}, userCmd...)
if needsAwsCredential((userCmd)) {
awsEnv, err := loadAwsCredentials(ctx)
Expand Down Expand Up @@ -142,8 +198,14 @@ func main() {
panic(err)
}
introspector := requireIntrospectorComposition(ctx, cli)
err = cmdPassthrough(ctx, cli, introspector, cmd)
cmds, err := unrollRunCommands(cmd)
if err != nil {
panic(err)
}
for _, cmd := range cmds {
err = cmdPassthrough(ctx, cli, introspector, cmd)
if err != nil {
panic(err)
}
}
}
2 changes: 1 addition & 1 deletion migrations/provider/aws/0023-aws_s3_bucket.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS aws_s3_bucket (
policy JSONB,
policystatus JSONB,
replication JSONB,
requestpayment TEXT,
requestpayment JSONB,
tagging JSONB,
versioning JSONB,
website JSONB,
Expand Down
6 changes: 6 additions & 0 deletions sample_queries/aws_ec2_instance_ips.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
uri,
instanceid,
publicipaddress
FROM
aws_ec2_instance
8 changes: 8 additions & 0 deletions sample_queries/aws_function_versions_with_aliases.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
DISTINCT(FV.version)
FROM
aws_lambda_alias AS A
INNER JOIN aws_lambda_alias_functionversion AS AFV
ON A._id = AFV.alias_id
INNER JOIN aws_lambda_functionversion AS FV
ON AFV.functionversion_id = FV._id
8 changes: 8 additions & 0 deletions sample_queries/aws_owner_pays_buckets.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
name,
uri,
requestpayment->>'Payer' AS Payer
FROM
aws_s3_bucket
WHERE
requestpayment->>'Payer' = 'BucketOwner'
8 changes: 8 additions & 0 deletions sample_queries/aws_policy_documents.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
P.uri AS PolicyArn,
PV.document AS PolicyDocument
FROM
aws_iam_policy AS P
INNER JOIN aws_iam_policyversion AS PV
ON PV._policy_id = P._id
AND PV.isdefaultversion = true
8 changes: 8 additions & 0 deletions sample_queries/aws_rds_check_if_backups_are_disabled.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT
uri,
dbinstanceidentifier,
backupretentionperiod
FROM
aws_rds_dbinstance
WHERE
backupretentionperiod = 0
6 changes: 6 additions & 0 deletions sample_queries/aws_storage_buckets.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
name,
uri,
creationdate
FROM
aws_s3_bucket
4 changes: 4 additions & 0 deletions sample_queries/aws_total_disk_size.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SELECT
SUM(size)
FROM
aws_ec2_volume

0 comments on commit b86838f

Please sign in to comment.