Last active
August 29, 2015 14:20
-
-
Save dustinmm80/6ab1edcd1d50617c53c8 to your computer and use it in GitHub Desktop.
Shelling out from Go to retrieve a keychain item's password
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
/* | |
./keychain -name mysecret | |
Siam589_logy | |
On run you'll get a popup window asking for access. | |
*/ | |
import ( | |
"flag" | |
"fmt" | |
"os/exec" | |
"strings" | |
) | |
func GetSecretValue(name string) (string, error) { | |
cmd := exec.Command( | |
"security", | |
"find-generic-password", | |
"-l", | |
name, | |
"-w", | |
) | |
out, err := cmd.CombinedOutput() | |
strippedOut := strings.TrimSpace(string(out)) | |
if err != nil { | |
return "", fmt.Errorf("%s\n%s", strippedOut, err) | |
} | |
return strippedOut, nil | |
} | |
func main() { | |
secretName := flag.String("name", "", "Name of the secret") | |
flag.Parse() | |
secretValue, err := GetSecretValue(*secretName) | |
if err != nil { | |
fmt.Println(err.Error()) | |
return | |
} | |
fmt.Print(secretValue) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment