| 21 | var ErrProfileNotFound = errors.New("profile not found") |
| 22 | |
| 23 | func PullToTmp( |
| 24 | ctx context.Context, |
| 25 | creds *devopt.Credentials, |
| 26 | profile string, |
| 27 | ) (string, error) { |
| 28 | config, err := assumeRole(ctx, creds) |
| 29 | if err != nil { |
| 30 | return "", err |
| 31 | } |
| 32 | |
| 33 | // TODO(landau), before pulling, ensure that the profile exists in the cloud |
| 34 | s3Client := manager.NewDownloader(s3.NewFromConfig(*config)) |
| 35 | buf := manager.WriteAtBuffer{} |
| 36 | |
| 37 | ux.Finfof( |
| 38 | os.Stderr, |
| 39 | "Logged in as %s, pulling from jetify cloud (profile: %s)\n", |
| 40 | creds.Email, |
| 41 | profile, |
| 42 | ) |
| 43 | |
| 44 | if _, err = s3Client.Download( |
| 45 | ctx, |
| 46 | &buf, |
| 47 | &s3.GetObjectInput{ |
| 48 | Bucket: aws.String(bucket), |
| 49 | Key: aws.String( |
| 50 | fmt.Sprintf( |
| 51 | "profiles/%s/%s.tar.gz", |
| 52 | creds.Sub, |
| 53 | profile, |
| 54 | ), |
| 55 | ), |
| 56 | }, |
| 57 | // TODO, we can use an s3 list objects to make this more accurate |
| 58 | ); err != nil && strings.Contains(err.Error(), "AccessDenied") { |
| 59 | return "", ErrProfileNotFound |
| 60 | } else if err != nil { |
| 61 | return "", errors.WithStack(err) |
| 62 | } |
| 63 | |
| 64 | dir, err := tar.Extract(buf.Bytes()) |
| 65 | if err != nil { |
| 66 | return "", err |
| 67 | } |
| 68 | |
| 69 | ux.Fsuccessf( |
| 70 | os.Stderr, |
| 71 | "Profile successfully pulled (profile: %s)\n", |
| 72 | profile, |
| 73 | ) |
| 74 | |
| 75 | return dir, nil |
| 76 | } |