Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Using https protocol not require ssh config #2438

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions cmd/fluxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
_ "net/http/pprof"
"net/url"
"os"
"os/exec"
"os/signal"
Expand Down Expand Up @@ -110,11 +111,12 @@ func main() {
kubernetesKubectl = fs.String("kubernetes-kubectl", "", "optional, explicit path to kubectl tool")
versionFlag = fs.Bool("version", false, "get version number")
// Git repo & key etc.
gitURL = fs.String("git-url", "", "URL of git repo with Kubernetes manifests; e.g., [email protected]:weaveworks/flux-get-started")
gitURL = fs.String("git-url", "", "URL of git repo with Kubernetes manifests; e.g., [email protected]:weaveworks/flux-get-started or https://github.com/fluxcd/flux.git . If specifying HTTPS transport, you can add git-user and git-apikey for authentication")
gitBranch = fs.String("git-branch", "master", "branch of git repo to use for Kubernetes manifests")
gitPath = fs.StringSlice("git-path", []string{}, "relative paths within the git repo to locate Kubernetes manifests")
gitReadonly = fs.Bool("git-readonly", false, fmt.Sprintf("use to prevent Flux from pushing changes to git; implies --sync-state=%s", fluxsync.NativeStateMode))
gitUser = fs.String("git-user", "Weave Flux", "username to use as git committer")
gitAPIKey = fs.String("git-apikey", "", `if set, git-apikey will be used to clone repo and push sync tags only using HTTPS protocol`)
gitEmail = fs.String("git-email", "[email protected]", "email to use as git committer")
gitSetAuthor = fs.Bool("git-set-author", false, "if set, the author of git commits will reflect the user who initiated the commit and will differ from the git committer.")
gitLabel = fs.String("git-label", "", "label to keep track of sync progress; overrides both --git-sync-tag and --git-notes-ref")
Expand Down Expand Up @@ -319,7 +321,7 @@ func main() {
}
}

if *sshKeygenDir == "" {
if *sshKeygenDir == "" && *gitAPIKey == "" {
logger.Log("info", fmt.Sprintf("SSH keygen dir (--ssh-keygen-dir) not provided, so using the deploy key volume (--k8s-secret-volume-mount-path=%s); this may cause problems if the deploy key volume is mounted read-only", *k8sSecretVolumeMountPath))
*sshKeygenDir = *k8sSecretVolumeMountPath
}
Expand Down Expand Up @@ -430,7 +432,7 @@ func main() {
}
clusterVersion = "kubernetes-" + serverVersion.GitVersion

if *k8sInCluster {
if *k8sInCluster && *gitAPIKey == "" {
namespace, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil {
logger.Log("err", err)
Expand Down Expand Up @@ -596,12 +598,42 @@ func main() {
}
checkpoint.CheckForUpdates(product, version, checkpointFlags, updateCheckLogger)

if *gitAPIKey != "" {
parsedGitURL, err := url.Parse(*gitURL)

if err != nil {
logger.Log("err", err)
os.Exit(1)
}

if parsedGitURL.Scheme != "https" {
logger.Log("err", "git-apikey argument can be used only with HTTPS transport")
os.Exit(1)
}

if parsedGitURL.User.Username() != "" {
logger.Log("err", "Set git username not in git-url, but use git-user argument instead")
Copy link
Member

@hiddeco hiddeco Sep 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The --git-user is not used in the way you think it is used, or at least, this implementation does not take into account that it is used as the full name of the user (i.e. John Doe <[email protected]>) during commit. While I think GitHub does some behind-the-scene parsing to still get this information presented in their UI based on the GitHub username, the data in git will be off.

However, given that we want this feature in in a short timeframe, I will volunteer to make the required changes.

os.Exit(1)
}

_, passwordExists := parsedGitURL.User.Password()

if passwordExists {
logger.Log("err", "Set git APIkey not in git-url, but use git-apikey argument instead")
os.Exit(1)
}

parsedGitURL.User = url.UserPassword(*gitUser, *gitAPIKey)
*gitURL = parsedGitURL.String()
}

gitRemote := git.Remote{URL: *gitURL}
gitConfig := git.Config{
Paths: *gitPath,
Branch: *gitBranch,
NotesRef: *gitNotesRef,
UserName: *gitUser,
APIKey: *gitAPIKey,
UserEmail: *gitEmail,
SigningKey: *gitSigningKey,
SetAuthor: *gitSetAuthor,
Expand All @@ -621,7 +653,7 @@ func main() {
}

logger.Log(
"url", *gitURL,
"url", gitRemote.SafeURL(),
"user", *gitUser,
"email", *gitEmail,
"signing-key", *gitSigningKey,
Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func (d *Daemon) NotifyChange(ctx context.Context, change v9.Change) error {
switch change.Kind {
case v9.GitChange:
gitUpdate := change.Source.(v9.GitUpdate)
if gitUpdate.URL != d.Repo.Origin().URL && gitUpdate.Branch != d.GitConfig.Branch {
if gitUpdate.URL != d.Repo.Origin().SafeURL() && gitUpdate.Branch != d.GitConfig.Branch {
// It isn't strictly an _error_ to be notified about a repo/branch pair
// that isn't ours, but it's worth logging anyway for debugging.
d.Logger.Log("msg", "notified about unrelated change",
Expand Down
5 changes: 2 additions & 3 deletions daemon/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,13 @@ func (d *Daemon) Loop(stop chan struct{}, wg *sync.WaitGroup, logger log.Logger)
cancel()

if err != nil {
logger.Log("url", d.Repo.Origin().URL, "err", err)
logger.Log("url", d.Repo.Origin().SafeURL(), "err", err)
continue
}
if invalidCommit.Revision != "" {
logger.Log("err", "found invalid GPG signature for commit", "revision", invalidCommit.Revision, "key", invalidCommit.Signature.Key)
}

logger.Log("event", "refreshed", "url", d.Repo.Origin().URL, "branch", d.GitConfig.Branch, "HEAD", newSyncHead)
logger.Log("event", "refreshed", "url", d.Repo.Origin().SafeURL(), "branch", d.GitConfig.Branch, "HEAD", newSyncHead)
if newSyncHead != syncHead {
syncHead = newSyncHead
d.AskForSync()
Expand Down
1 change: 1 addition & 0 deletions git/working.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Config struct {
Paths []string // paths within the repo containing files we care about
NotesRef string
UserName string
APIKey string
UserEmail string
SigningKey string
SetAuthor bool
Expand Down
2 changes: 1 addition & 1 deletion ssh/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type KeyRing interface {
type sshKeyRing struct{}

// NewNopSSHKeyRing returns a KeyRing that doesn't do anything.
// It is meant for local development purposes when running fluxd outside a Kubernetes container.
// It is meant for local development purposes when running fluxd outside a Kubernetes container or authentication to git done via HTTPS
func NewNopSSHKeyRing() KeyRing {
return &sshKeyRing{}
}
Expand Down