Skip to content

Commit

Permalink
Merge branch 'main' of github.com:Azure/draftv2 into karenchen/flag-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
karenychen committed May 16, 2022
2 parents d89219f + edc788f commit 8f760a3
Show file tree
Hide file tree
Showing 25 changed files with 652 additions and 86 deletions.
330 changes: 275 additions & 55 deletions .github/workflows/integration-linux.yml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Microsoft Open Source Code of Conduct

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).

Resources:

- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Contributing

This project welcomes contributions and suggestions. Most contributions require you to
agree to a Contributor License Agreement (CLA) declaring that you have the right to,
and actually do, grant us the rights to use your contribution. For details, visit
https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need
to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the
instructions provided by the bot. You will only need to do this once across all repositories using our CLA.

This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ Draft requires Go version 1.18.x.

Distributed under the MIT License. See [LISENCE](https://github.com/Azure/draft/blob/main/LICENSE) for more information.

## Trademark Notice
Trademarks This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow Microsoft’s Trademark & Brand Guidelines. Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party’s policies.

41 changes: 41 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!-- BEGIN MICROSOFT SECURITY.MD V0.0.5 BLOCK -->

## Security

Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/).

If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://docs.microsoft.com/en-us/previous-versions/tn-archive/cc751383(v=technet.10)), please report it to us as described below.

## Reporting Security Issues

**Please do not report security vulnerabilities through public GitHub issues.**

Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://msrc.microsoft.com/create-report).

If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://www.microsoft.com/en-us/msrc/pgp-key-msrc).

You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).

Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:

* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
* Full paths of source file(s) related to the manifestation of the issue
* The location of the affected source code (tag/branch/commit or direct URL)
* Any special configuration required to reproduce the issue
* Step-by-step instructions to reproduce the issue
* Proof-of-concept or exploit code (if possible)
* Impact of the issue, including how an attacker might exploit the issue

This information will help us triage your report more quickly.

If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://microsoft.com/msrc/bounty) page for more details about our active programs.

## Preferred Languages

We prefer all communications to be in English.

## Policy

Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://www.microsoft.com/en-us/msrc/cvd).

<!-- END MICROSOFT SECURITY.MD BLOCK -->
2 changes: 1 addition & 1 deletion cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (cc *createCmd) initConfig() error {
return err
}

viper.SetConfigFile("yaml")
viper.SetConfigType("yaml")
if err = viper.ReadConfig(bytes.NewBuffer(configBytes)); err != nil {
return err
}
Expand Down
123 changes: 123 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package cmd

import (
"fmt"
"io/ioutil"
"os"
"strings"
"testing"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/languages"
"github.com/Azure/draft/pkg/linguist"
)

func TestRun(t *testing.T) {
mockCC := &createCmd{}
mockCC.createConfig = &config.CreateConfig{}
mockCC.dest = "./.."
mockCC.createConfig.DeployType = "helm"
mockCC.createConfig.LanguageVariables = []config.UserInputs{}
mockCC.createConfig.DeployVariables = []config.UserInputs{}
mockPortInput := config.UserInputs{Name: "PORT", Value: "8080"}
mockAppNameInput := config.UserInputs{Name: "APPNAME", Value: "testingCreateCommand"}
mockCC.createConfig.DeployVariables = append(mockCC.createConfig.DeployVariables, mockPortInput, mockAppNameInput)
mockCC.createConfig.LanguageVariables = append(mockCC.createConfig.LanguageVariables, mockPortInput)

oldDockerfile, _ := ioutil.ReadFile("./../Dockerfile")
oldDockerignore, _ := ioutil.ReadFile("./../.dockerignore")

detectedLang, lowerLang, err := mockCC.mockDetectLanguage()

assert.False(t, detectedLang == nil)
assert.False(t, lowerLang == "")
assert.True(t, err == nil)

err = mockCC.generateDockerfile(detectedLang, lowerLang)
assert.True(t, err == nil)


err = mockCC.createDeployment()
assert.True(t, err == nil)
err = ioutil.WriteFile("./../Dockerfile", oldDockerfile, 0644)
if err != nil {
t.Error(err)
}

err = ioutil.WriteFile("./../.dockerignore", oldDockerignore, 0644)
if err != nil {
t.Error(err)
}

os.RemoveAll("./../charts")
}

func TestInitConfig(t *testing.T) {
mockCC := &createCmd{}
mockCC.createConfig = &config.CreateConfig{}
mockCC.dest = "./.."
mockCC.createConfigPath = "./../test/templates/config.yaml"

err := mockCC.initConfig()
assert.True(t, err == nil)
assert.True(t, mockCC.createConfig != nil)
}

func (mcc *createCmd) mockDetectLanguage() (*config.DraftConfig, string, error) {
hasGo := false
hasGoMod := false
var langs []*linguist.Language
var err error

if mcc.createConfig.LanguageType == "" {
langs, err = linguist.ProcessDir(mcc.dest)
log.Debugf("linguist.ProcessDir(%v) result:\n\nError: %v", mcc.dest, err)
if err != nil {
return nil, "", fmt.Errorf("there was an error detecting the language: %s", err)
}

for _, lang := range langs {
log.Debugf("%s:\t%f (%s)", lang.Language, lang.Percent, lang.Color)
}

log.Debugf("detected %d langs", len(langs))

if len(langs) == 0 {
return nil, "", ErrNoLanguageDetected
}
}

mcc.supportedLangs = languages.CreateLanguages(mcc.dest)

if mcc.createConfig.LanguageType != "" {
log.Debug("using configuration language")
lowerLang := strings.ToLower(mcc.createConfig.LanguageType)
langConfig := mcc.supportedLangs.GetConfig(lowerLang)
if langConfig == nil {
return nil, "", ErrNoLanguageDetected
}

return langConfig, lowerLang, nil
}

for _, lang := range langs {
detectedLang := linguist.Alias(lang)
log.Infof("--> Draft detected %s (%f%%)\n", detectedLang.Language, detectedLang.Percent)
lowerLang := strings.ToLower(detectedLang.Language)

if mcc.supportedLangs.ContainsLanguage(lowerLang) {
if lowerLang == "go" && hasGo && hasGoMod {
log.Debug("detected go and go module")
lowerLang = "gomodule"
}

langConfig := mcc.supportedLangs.GetConfig(lowerLang)
return langConfig, lowerLang, nil
}
log.Infof("--> Could not find a pack for %s. Trying to find the next likely language match...\n", detectedLang.Language)
}
return nil, "", ErrNoLanguageDetected
}
16 changes: 16 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRootInitConfig(t *testing.T) {
initConfig()

cfgFile := "./../test/templates/config.yaml"
initConfig()

assert.True(t, cfgFile != "")
}
8 changes: 4 additions & 4 deletions cmd/setup-gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ application and service principle, and will configure that application to trust
RunE: func(cmd *cobra.Command, args []string) error {
fillSetUpConfig(sc)

s := spinner.GetSpinner("--> Setting up Github OIDC...")
s := spinner.CreateSpinner("--> Setting up Github OIDC...")
s.Start()
err := runProviderSetUp(sc)
err := runProviderSetUp(sc, s)
s.Stop()
if err != nil {
return err
Expand Down Expand Up @@ -76,11 +76,11 @@ func fillSetUpConfig(sc *providers.SetUpCmd) {
}
}

func runProviderSetUp(sc *providers.SetUpCmd) error {
func runProviderSetUp(sc *providers.SetUpCmd, s spinner.Spinner) error {
provider := strings.ToLower(sc.Provider)
if provider == "azure" {
// call azure provider logic
return providers.InitiateAzureOIDCFlow(sc)
return providers.InitiateAzureOIDCFlow(sc, s)

} else {
// call logic for user-submitted provider
Expand Down
26 changes: 26 additions & 0 deletions cmd/setup-gh_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/Azure/draft/pkg/providers"
"github.com/Azure/draft/pkg/spinner"
)

func TestSetUpConfig(t *testing.T) {
mockSetUpCmd := &providers.SetUpCmd{}
mockSetUpCmd.AppName = "testingSetUpCommand"
mockSetUpCmd.Provider = "Google"
mockSetUpCmd.Repo = "test/repo"
mockSetUpCmd.ResourceGroupName = "testResourceGroup"
mockSetUpCmd.SubscriptionID = "123456789"
s := spinner.CreateSpinner("--> Setting up Github OIDC...")

fillSetUpConfig(mockSetUpCmd)

err := runProviderSetUp(mockSetUpCmd, s)

assert.True(t, err == nil)
}
12 changes: 6 additions & 6 deletions deployTypes/kustomize/base/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
name: {{APPNAME}}
labels:
app: my-app
app: {{APPNAME}}
spec:
replicas: 1
selector:
matchLabels:
app: my-app
app: {{APPNAME}}
template:
metadata:
labels:
app: my-app
app: {{APPNAME}}
spec:
containers:
- name: my-app
image: my-app
- name: {{APPNAME}}
image: {{APPNAME}}:latest
ports:
- containerPort: {{PORT}}
4 changes: 2 additions & 2 deletions deployTypes/kustomize/base/service.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: my-app
name: {{APPNAME}}
spec:
type: LoadBalancer
selector:
app: my-app
app: {{APPNAME}}
ports:
- protocol: TCP
port: 80
Expand Down
6 changes: 3 additions & 3 deletions deployTypes/kustomize/overlays/production/deployment.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
name: {{APPNAME}}
labels:
app: my-app
app: {{APPNAME}}
spec:
template:
spec:
containers:
- name: my-app
- name: {{APPNAME}}
image: {{APPNAME}}
2 changes: 1 addition & 1 deletion deployTypes/kustomize/overlays/production/service.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apiVersion: v1
kind: Service
metadata:
name: my-app
name: {{APPNAME}}
spec:
type: LoadBalancer
4 changes: 2 additions & 2 deletions pkg/deployments/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (d *Deployments) loadConfig(lang string) (*config.DraftConfig, error) {
return &draftConfig, nil
}

func (d *Deployments) GetConfig(deployTyoe string) *config.DraftConfig {
val, ok := d.configs[deployTyoe]
func (d *Deployments) GetConfig(deployType string) *config.DraftConfig {
val, ok := d.configs[deployType]
if !ok {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/languages/languages.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (l *Languages) loadConfig(lang string) (*config.DraftConfig, error) {
return nil, err
}

viper.SetConfigFile("yaml")
viper.SetConfigType("yaml")
if err = viper.ReadConfig(bytes.NewBuffer(configBytes)); err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 8f760a3

Please sign in to comment.