azure ad cli

https://teamsmiley.gitbook.io/devops/azure/active-directory

์—ฌ๊ธฐ์— ์ด์–ด์„œ ๋งŒ๋“ ๋‹ค

๊ธฐ์กด cli๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. https://teamsmiley.gitbook.io/devops/go-lang/create-cli

ํ† ํฐ ๋ฐ›์•„์˜ค๊ธฐ

package httpApi

import (
  "context"
  "log"

  msal "github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential"
  "github.com/spf13/viper"
)

const scope = "api://xxxxx/.default"
const clientID = "xxxxx"

func getSecret() string {
  sec := viper.GetString("AZURE_SECRET")
  if sec == "" {
    log.Fatalf("AZURE_SECRET is missing, please configure it myctl configure --secret <secret>")
  }
  return sec
}

type tokenProvider struct{}

func (t *tokenProvider) OAuthToken() string {

  clientCredential, err := msal.NewCredFromSecret(getSecret())

  if err != nil {
    log.Fatalf("Couldn't create client app (%s)", err)
  }
  app, err := msal.New(clientID, clientCredential, func(o *msal.Options) {
    o.Authority = "https://login.microsoftonline.com/xxxxxxx"
  })
  if err != nil {
    log.Fatalf("Couldn't get application (%s)", err)
  }

  ctx := context.Background()
  token, err := app.AcquireTokenByCredential(ctx, []string{
    scope,
  })
  if err != nil {
    log.Fatalf("Error getting token (%s)", err)
  }
  return token.AccessToken
}

xxxx๋œ ๋ถ€๋ถ„์„ ์ „๋ถ€ ์ˆ˜์ •ํ•ด์ค€๋‹ค.

์ด์ œ http ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ๋ณด๋‚ด๊ธฐ ์ „์— ํ˜ธ์ถœํ•ด์„œ ํ† ํฐ์„ ๋งŒ๋“ ํ›„ ํ† ํฐ์„ http ๋ฆฌํ€˜์ŠคํŠธ์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

package httpApi

import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "net/http"
)

// json ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
type ServerResponse struct {
  Id                      string `json:"id"`
  Memo                    string `json:"memo"`
  Cpu                     string `json:"cpu"`
  Memory                  *int   `json:"memory"`
  CpuModelName            string `json:"cpuModelName"`
}

func GetServer(query string) []ServerResponse {
  searchApi := "https://api.xxx.com/servers"

  oauthTokenProvider := new(tokenProvider) // auth ์ƒ์„ฑ

  t := oauthTokenProvider.OAuthToken() // ํ† ํฐ ๋ฐœ๊ธ‰

  r, err := http.NewRequest(http.MethodGet, searchApi, nil) // http request ์ƒ์„ฑ
  if err != nil {
    panic(err)
  }

  r.Header.Add("Content-Type", "application/json") //ํ—ค๋”์— ์ถ”๊ฐ€
  r.Header.Add("Authorization", "Bearer " + t) //ํ—ค๋”์— ํ† ํฐ ์ถ”๊ฐ€

  // appending to existing query args
  q := r.URL.Query()
  q.Add("searchquery", query)

  // assign encoded query string to http request
  r.URL.RawQuery = q.Encode()

  resp, err := http.DefaultClient.Do(r)

  if err != nil {
    panic(err)
  }

  defer resp.Body.Close()

  responseBody, err := ioutil.ReadAll(resp.Body) // ์‘๋‹ต ๊ฒฐ๊ณผ ์ฝ๊ธฐ

  if err != nil {
    panic(err)
  }

  fmt.Println("status : ", resp.Status)
  fmt.Println("responseBody : ", string(responseBody))

  // var result Server
  var jsonData []ServerResponse
  err = json.Unmarshal(responseBody, &jsonData) // json ํŒŒ์‹ฑ
  if err != nil {
    fmt.Println("http response unmarshal error", err)
    panic(err)
  }

  // fmt.Printf("result : %+v", jsonData) //๊ฒฐ๊ณผ ํ™•์ธ

  return jsonData
}

์ด์ œ myctl์„ ์‚ฌ์šฉํ•˜๋ฉด ์–ธ์ œ๋“  ํ† ํฐ์„ ๋ฐ›์•„์™€์„œ ๊ทธ ํ† ํฐ์„ ์ด์šฉํ•˜์—ฌ api์— ์š”์ฒญํ• ์ˆ˜ ์žˆ๋‹ค.

์ด๊ฑธ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ œ api์— ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„์ˆ˜๋„ ์“ธ์ˆ˜๋„ ์žˆ๊ฒŒ ๋œ๋‹ค.

Last updated