maz

package module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 20 Imported by: 0

README

maz

Microsoft Azure library module for simple MSAL authentication, and calling MS Graph and Azure resource APIs. Other APIs could be added in the future.

WARNING: Currently under constant changes.

Getting Started

  1. Any program or utility wanting to use this libray module can simply import it, then instantiate a variable of type maz.Bundle to manage the interaction. For example:
import (
    "github.com/git719/maz"
)
z := maz.Bundle{
    ConfDir:      "",                   // Set up later, see example below
    CredsFile:    "credentials.yaml",
    TokenFile:    "accessTokens.json",
    TenantId:     "",
    ClientId:     "",
    ClientSecret: "",
    Interactive:  false,
    Username:     "",
    AuthorityUrl: "",                   // Set up later with maz.ConstAuthUrl + z.TenantId (see const block in maz.go)
    MgToken:      "",                   // Set up below 4 later with function maz.SetupApiTokens()
    MgHeaders:    map[string]string{},
    AzToken:      "",
    AzHeaders:    map[string]string{},  
}
// Then update the variables within the Bundle, to set up configuration directory
z.ConfDir = filepath.Join(os.Getenv("HOME"), "." + prgname)
if utl.FileNotExist(z.ConfDir) {
    if err := os.Mkdir(z.ConfDir, 0700); err != nil {
        panic(err.Error())
    }
}
  1. Then call maz.SetupInterativeLogin(z) or maz.SetupAutomatedLogin(z) to setup the credentials file accordingly.
  2. Then call z := maz.SetupApiTokens(*z) to acquire the respective API tokens, web headers, and other variables.
  3. Now call whatever MS Graph and Azure Resource API functions you want by passing and using the z variables, with its z.mgHeaders and/or z.azHeaders attributes, and so on.

Login Credentials

There are four (4) different ways to set up the login credentials to use this library module. All four ways required three (3) special attributes:

# Type Method Details
1 Interactive Config file Set up attributes via ~/.maz/credentials.yaml file
2 Interactive Environment variables Set up attributes via environment variables (OVERIDES config file)
3 Automated Config file Set up attributes via ~/.maz/credentials.yaml file
4 Automated Environment variables Set up attributes via environment variables (OVERIDES config file)
  1. Interactive via config file: The calling utility sets up a way to allow setting up the ~/.maz/credentials.yaml file with the 3 special attributes. For example, the zls CLI utility does this via the -cri switch, to Set up MSAL interactive browser popup login:
    zls -cri 3f050090-20b0-40a0-a060-c05060104010 user1@domain.io
    
    Above will populate the ~/.maz/credentials.yaml file as follows:
    tenant_id: 3f050090-20b0-40a0-a060-c05060104010
    username: user1@domain.io
    interactive: true
    
    From then on the zls utility will use above credentials to interact with the maz library to perform all its functions.
  2. Interactive via environment variables: The calling utility will instead use the os.Getenv("VAR") function to look for the following 3 special environment variables:
    MAZ_TENANT_ID=3f050090-20b0-40a0-a060-c05060104010
    MAZ_USERNAME=user1@domain.io
    MAZ_INTERACTIVE=true
    
    Above values take precedence and OVERIDE any existing config ~/.maz/credentials.yaml file values.
  3. Automated via config file: The calling utility sets up a way to allow setting up the ~/.maz/credentials.yaml file with the 3 special attributes. For example, the zls CLI utility does this via the -cr switch, to Set up MSAL automated ClientId + Secret login:
    zls -cr 3f050090-20b0-40a0-a060-c05060104010 f1110121-7111-4171-a181-e1614131e181 ACB8c~HdLejfQGiHeI9LUKgNOODPQRISNTmVLX_i
    
    Above will populate the ~/.maz/credentials.yaml file as follows:
    tenant_id: 3f050090-20b0-40a0-a060-c05060104010
    client_id: f1110121-7111-4171-a181-e1614131e181
    client_secret: ACB8c~HdLejfQGiHeI9LUKgNOODPQRISNTmVLX_i
    
    From then on the zls utility will use above credentials to interact with the maz library to perform all its functions.
  4. Automated via environment variables: The calling utility will instead use the os.Getenv("VAR") function to look for the following 3 special environment variables
    MAZ_TENANT_ID=3f050090-20b0-40a0-a060-c05060104010
    MAZ_CLIENT_ID=f1110121-7111-4171-a181-e1614131e181
    MAZ_CLIENT_SECRET=ACB8c~HdLejfQGiHeI9LUKgNOODPQRISNTmVLX_i
    
    Above values take precedence and OVERIDE any existing config ~/.maz/credentials.yaml file values.

The benefit of using environment variables is of course to be able to override an existing credentials.yaml file, and specify different credentials, as well as being able to do this from different shell sessions on the same host. They also allow utilities written with this library to be more used in continuous delivery and other types of automation.

NOTE: If all four MAZ_USERNAME, MAZ_INTERACTIVE, MAZ_CLIENT_ID, and MAZ_CLIENT_SECRET are properly define, then precedence is given to the Username Interactive login. To force a ClientID ClientSecret login via environment variables, you must ensure the first two are unset in the current shell.

Functions

List of all available functions.

  • maz.SetupInterativeLogin: This functions allows you to set up the~/.maz/credentials.yaml file for interactive Azure login.

TODO: List other functions here ...

Documentation

Index

Constants

View Source
const (
	ConstAuthUrl = "https://login.microsoftonline.com/"
	ConstMgUrl   = "https://graph.microsoft.com"
	ConstAzUrl   = "https://management.azure.com"

	ConstAzPowerShellClientId = "1950a258-227b-4e31-a9cf-717495945fc2" // 'Microsoft Azure PowerShell' ClientId

	// See https://stackoverflow.com/questions/1508490/erase-the-current-printed-console-line
	ConstCacheFileExtension   = "gz"
	ConstMgCacheFileAgePeriod = 1800  // Half hour
	ConstAzCacheFileAgePeriod = 86400 // One day
)

Variables

This section is empty.

Functions

func AdRolesCountAzure added in v0.8.0

func AdRolesCountAzure(z Bundle) int64

func AdRolesCountLocal added in v0.8.0

func AdRolesCountLocal(z Bundle) int64

func AddAppSecret added in v0.10.0

func AddAppSecret(uuid, displayName, expiry string, z Bundle)

func AddSpSecret added in v0.10.0

func AddSpSecret(uuid, displayName, expiry string, z Bundle)

func ApiCall added in v0.8.0

func ApiCall(method, url string, z Bundle, payload jsonT, params strMapT, verbose bool) (result jsonT, rsc int, err error)

func ApiDelete added in v0.8.8

func ApiDelete(url string, z Bundle, params strMapT) (result jsonT, rsc int, err error)

func ApiDeleteDebug added in v0.8.8

func ApiDeleteDebug(url string, z Bundle, params strMapT) (result jsonT, rsc int, err error)

func ApiErrorCheck added in v0.8.0

func ApiErrorCheck(method, url, caller string, r jsonT)

func ApiGet added in v0.8.0

func ApiGet(url string, z Bundle, params strMapT) (result jsonT, rsc int, err error)

func ApiGetDebug added in v0.8.0

func ApiGetDebug(url string, z Bundle, params strMapT) (result jsonT, rsc int, err error)

func ApiPost added in v0.10.0

func ApiPost(url string, z Bundle, payload jsonT, params strMapT) (result jsonT, rsc int, err error)

func ApiPostDebug added in v0.10.0

func ApiPostDebug(url string, z Bundle, payload jsonT, params strMapT) (result jsonT, rsc int, err error)

func ApiPut added in v0.8.8

func ApiPut(url string, z Bundle, payload jsonT, params strMapT) (result jsonT, rsc int, err error)

func ApiPutDebug added in v0.8.8

func ApiPutDebug(url string, z Bundle, payload jsonT, params strMapT) (result jsonT, rsc int, err error)

func AppsCountAzure added in v0.8.0

func AppsCountAzure(z Bundle) int64

func AppsCountLocal added in v0.8.0

func AppsCountLocal(z Bundle) int64

func CompareSpecfileToAzure added in v0.8.0

func CompareSpecfileToAzure(filePath string, z Bundle)

func CreateAzRoleAssignment added in v0.8.8

func CreateAzRoleAssignment(x map[string]interface{}, z Bundle)

func CreateSkeletonFile added in v0.8.7

func CreateSkeletonFile(t string)

func DecodeJwtToken added in v0.9.9

func DecodeJwtToken(tokenString string)

func DeleteAzObject added in v0.8.8

func DeleteAzObject(specifier string, z Bundle)

func DeleteAzRoleAssignmentByFqid added in v0.8.8

func DeleteAzRoleAssignmentByFqid(fqid string, z Bundle) map[string]interface{}

func DeleteAzRoleDefinitionByFqid added in v0.8.8

func DeleteAzRoleDefinitionByFqid(fqid string, z Bundle) map[string]interface{}

func DumpLoginValues added in v0.12.0

func DumpLoginValues(z Bundle)

func DumpRuntimeValues added in v0.12.0

func DumpRuntimeValues(z Bundle)

func FindAzObjectsByUuid added in v0.8.8

func FindAzObjectsByUuid(uuid string, z Bundle) (list []interface{})

func GetAzAdRoleByUuid added in v0.8.8

func GetAzAdRoleByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzAdRoles added in v0.8.0

func GetAzAdRoles(z Bundle, verbose bool) (list []interface{})

func GetAzAllPages added in v1.4.0

func GetAzAllPages(url string, z Bundle) (list []interface{})

func GetAzAppByUuid added in v0.8.8

func GetAzAppByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzApps added in v0.8.0

func GetAzApps(z Bundle, verbose bool) (list []interface{})

func GetAzGroupByUuid added in v0.8.8

func GetAzGroupByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzGroups added in v0.8.0

func GetAzGroups(z Bundle, verbose bool) (list []interface{})

func GetAzMgGroups added in v0.8.0

func GetAzMgGroups(z Bundle) (list []interface{})

func GetAzObjectByUuid added in v0.8.8

func GetAzObjectByUuid(t, uuid string, z Bundle) (x map[string]interface{})

func GetAzObjects added in v0.8.0

func GetAzObjects(url string, z Bundle, verbose bool) (deltaSet []interface{}, deltaLinkMap map[string]interface{})

func GetAzRbacScopes added in v0.8.0

func GetAzRbacScopes(z Bundle) (scopes []string)

func GetAzRoleAssignmentByObject added in v0.8.8

func GetAzRoleAssignmentByObject(x map[string]interface{}, z Bundle) (y map[string]interface{})

func GetAzRoleAssignmentByUuid added in v0.8.8

func GetAzRoleAssignmentByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzRoleAssignments added in v0.8.0

func GetAzRoleAssignments(z Bundle, verbose bool) (list []interface{})

func GetAzRoleDefinitionByName added in v0.8.8

func GetAzRoleDefinitionByName(roleName string, z Bundle) (y map[string]interface{})

func GetAzRoleDefinitionByObject added in v0.8.8

func GetAzRoleDefinitionByObject(x map[string]interface{}, z Bundle) (y map[string]interface{})

func GetAzRoleDefinitionByUuid added in v0.8.8

func GetAzRoleDefinitionByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzRoleDefinitions added in v0.8.0

func GetAzRoleDefinitions(z Bundle, verbose bool) (list []interface{})

func GetAzSpByUuid added in v0.8.8

func GetAzSpByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzSps added in v0.8.0

func GetAzSps(z Bundle, verbose bool) (list []interface{})

func GetAzSubscriptionByUuid added in v0.8.8

func GetAzSubscriptionByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzSubscriptions added in v0.8.0

func GetAzSubscriptions(z Bundle) (list []interface{})

func GetAzSubscriptionsIds added in v0.8.0

func GetAzSubscriptionsIds(z Bundle) (scopes []string)

func GetAzUserByUuid added in v0.8.8

func GetAzUserByUuid(uuid string, z Bundle) map[string]interface{}

func GetAzUsers added in v0.8.0

func GetAzUsers(z Bundle, verbose bool) (list []interface{})

func GetCachedObjects added in v1.0.0

func GetCachedObjects(cacheFile string) (cachedList []interface{})

func GetIdMapApps added in v0.8.0

func GetIdMapApps(z Bundle) (nameMap map[string]string)

func GetIdMapGroups added in v0.8.0

func GetIdMapGroups(z Bundle) (nameMap map[string]string)

func GetIdMapMgGroups added in v0.8.8

func GetIdMapMgGroups(z Bundle) (nameMap map[string]string)

func GetIdMapRoleDefs added in v0.8.0

func GetIdMapRoleDefs(z Bundle) (nameMap map[string]string)

func GetIdMapSps added in v0.8.0

func GetIdMapSps(z Bundle) (nameMap map[string]string)

func GetIdMapSubs added in v0.8.0

func GetIdMapSubs(z Bundle) (nameMap map[string]string)

func GetIdMapUsers added in v0.8.0

func GetIdMapUsers(z Bundle) (nameMap map[string]string)

func GetMatchingAdRoles added in v1.3.1

func GetMatchingAdRoles(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingApps added in v1.3.1

func GetMatchingApps(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingGroups added in v1.3.1

func GetMatchingGroups(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingMgGroups added in v1.3.1

func GetMatchingMgGroups(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingRoleAssignments added in v1.3.1

func GetMatchingRoleAssignments(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingRoleDefinitions added in v1.3.1

func GetMatchingRoleDefinitions(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingSps added in v1.3.1

func GetMatchingSps(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingSubscriptions added in v1.3.1

func GetMatchingSubscriptions(filter string, force bool, z Bundle) (list []interface{})

func GetMatchingUsers added in v1.3.1

func GetMatchingUsers(filter string, force bool, z Bundle) (list []interface{})

func GetObjectFromFile added in v0.8.0

func GetObjectFromFile(filePath string) (formatType, t string, obj map[string]interface{})

func GetObjects added in v0.8.0

func GetObjects(t, filter string, force bool, z Bundle) (list []interface{})

func GetTokenByCredentials

func GetTokenByCredentials(scopes []string, confDir, tokenFile, authorityUrl, clientId, clientSecret string) (token string, err error)

func GetTokenInteractively

func GetTokenInteractively(scopes []string, confDir, tokenFile, authorityUrl, username string) (token string, err error)

func GroupsCountAzure added in v0.8.0

func GroupsCountAzure(z Bundle) int64

func GroupsCountLocal added in v0.8.0

func GroupsCountLocal(z Bundle) int64

func MgGroupCountAzure added in v0.8.0

func MgGroupCountAzure(z Bundle) int64

func MgGroupCountLocal added in v0.8.0

func MgGroupCountLocal(z Bundle) int64

func MgType added in v0.8.0

func MgType(typeIn string) string

func NormalizeCache added in v0.8.0

func NormalizeCache(baseSet, deltaSet []interface{}) (list []interface{})

func PrintAdRole added in v0.8.0

func PrintAdRole(x map[string]interface{}, z Bundle)

func PrintApiErrMsg added in v0.12.0

func PrintApiErrMsg(msg string)

func PrintApp added in v0.8.0

func PrintApp(x map[string]interface{}, z Bundle)

func PrintAppRoleAssignmentsOthers added in v1.4.0

func PrintAppRoleAssignmentsOthers(appRoleAssignments []interface{}, z Bundle)

func PrintAppRoleAssignmentsSp added in v1.4.0

func PrintAppRoleAssignmentsSp(roleNameMap map[string]string, appRoleAssignments []interface{})

func PrintCertificateList added in v0.9.10

func PrintCertificateList(certificates []interface{})

func PrintCountStatus added in v0.8.0

func PrintCountStatus(z Bundle)

func PrintGroup added in v0.8.0

func PrintGroup(x map[string]interface{}, z Bundle)

func PrintHeaders added in v0.9.2

func PrintHeaders(headers http.Header)

func PrintMatching added in v0.9.13

func PrintMatching(printFormat, t, specifier string, z Bundle)

func PrintMemberOfs added in v0.8.0

func PrintMemberOfs(t string, memberOf []interface{})

func PrintMgChildren added in v0.8.0

func PrintMgChildren(indent int, children []interface{})

func PrintMgGroup added in v0.8.0

func PrintMgGroup(x map[string]interface{})

func PrintMgTree added in v0.8.0

func PrintMgTree(z Bundle)

func PrintObject added in v0.8.0

func PrintObject(t string, x map[string]interface{}, z Bundle)

func PrintObjectByUuid added in v0.8.8

func PrintObjectByUuid(uuid string, z Bundle)

func PrintOwners added in v0.9.10

func PrintOwners(owners []interface{})

func PrintPags added in v0.8.0

func PrintPags(z Bundle)

func PrintParams added in v0.9.2

func PrintParams(params url.Values)

func PrintRoleAssignment added in v0.8.0

func PrintRoleAssignment(x map[string]interface{}, z Bundle)

func PrintRoleAssignmentReport added in v0.8.0

func PrintRoleAssignmentReport(z Bundle)

func PrintRoleDefinition added in v0.8.0

func PrintRoleDefinition(x map[string]interface{}, z Bundle)

func PrintSecretList added in v0.9.10

func PrintSecretList(pwdCreds []interface{})

func PrintSp added in v0.8.0

func PrintSp(x map[string]interface{}, z Bundle)

func PrintStringMapColor added in v0.9.3

func PrintStringMapColor(strMap map[string]string)

func PrintSubscription added in v0.8.0

func PrintSubscription(x map[string]interface{})

func PrintTersely added in v0.8.0

func PrintTersely(t string, object interface{})

func PrintUser added in v0.8.0

func PrintUser(x map[string]interface{}, z Bundle)

func RemoveAppSecret added in v0.10.0

func RemoveAppSecret(uuid, keyId string, z Bundle)

func RemoveCacheFile added in v0.8.0

func RemoveCacheFile(t string, z Bundle)

func RemoveSpSecret added in v0.10.0

func RemoveSpSecret(uuid, keyId string, z Bundle)

func RoleAssignmentsCountAzure added in v0.8.0

func RoleAssignmentsCountAzure(z Bundle) int64

func RoleAssignmentsCountLocal added in v0.8.0

func RoleAssignmentsCountLocal(z Bundle) int64

func RoleDefinitionCountAzure added in v0.8.0

func RoleDefinitionCountAzure(z Bundle) (builtin, custom int64)

func RoleDefinitionCountLocal added in v0.8.0

func RoleDefinitionCountLocal(z Bundle) (builtin, custom int64)

func SelectObject added in v0.8.0

func SelectObject(id string, objSet []interface{}) (x map[string]interface{})

func SetupAutomatedLogin

func SetupAutomatedLogin(z Bundle)

func SetupInterativeLogin

func SetupInterativeLogin(z Bundle)

func SpsCountAzure added in v0.8.0

func SpsCountAzure(z Bundle) (native, microsoft int64)

func SpsCountLocal added in v0.8.0

func SpsCountLocal(z Bundle) (native, microsoft int64)

func SubsCountAzure added in v0.8.0

func SubsCountAzure(z Bundle) int64

func SubsCountLocal added in v0.8.0

func SubsCountLocal(z Bundle) int64

func UpsertAzObject added in v0.8.8

func UpsertAzObject(filePath string, z Bundle)

func UpsertAzRoleDefinition added in v0.8.8

func UpsertAzRoleDefinition(x map[string]interface{}, z Bundle)

func UsersCountAzure added in v0.8.0

func UsersCountAzure(z Bundle) int64

func UsersCountLocal added in v0.8.0

func UsersCountLocal(z Bundle) int64

Types

type Bundle

type Bundle struct {
	ConfDir      string // Directory where utility will store all its file
	CredsFile    string
	TokenFile    string
	TenantId     string
	ClientId     string
	ClientSecret string
	Interactive  bool
	Username     string
	AuthorityUrl string
	MgToken      string // This and below to support MS Graph API
	MgHeaders    map[string]string
	AzToken      string // This and below to support Azure Resource Management API
	AzHeaders    map[string]string
}

func SetupApiTokens

func SetupApiTokens(z *Bundle) Bundle

func SetupCredentials

func SetupCredentials(z *Bundle) Bundle

type TokenCache

type TokenCache struct {
	// contains filtered or unexported fields
}

func (*TokenCache) Export

func (t *TokenCache) Export(ctx context.Context, cache cache.Marshaler, hints cache.ExportHints) error

func (*TokenCache) Print added in v1.3.0

func (t *TokenCache) Print() string

func (*TokenCache) Replace

func (t *TokenCache) Replace(ctx context.Context, cache cache.Unmarshaler, hints cache.ReplaceHints) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL