Thursday, July 23, 2020

Introduction to Design patterns

what exactly are design patterns and principles?

One of the most important aspects of software development is that the code must be very much extendable without causing critical problems and also it should be easily maintainable. but not many of us focus on developing a software with these two aspects in our mind. we just plan to meet the deadline and somehow get the software working according to customer requirements. But little do we realize that the changes in requirements will give us a hell of time in changing our code.

This in turn will become a cycle. we spend such a lot of time reworking on the same thing even for some minor change in the requirement. (also called as "Technical Debt")

welcome "design patterns and design principles" !!!!  these two will reduce the "one hell of a time" which we are spending here and lets us focus on other important things like quality and testing.



we all know About the software development life cycle. we know how important design is in the development of a software. one small mistake and the whole software is in the docks.
so we will have to be very careful in design phase.
but this is the phase where in we can never recognize that we are going wrong.
this phase requires a long foresight into the problem which may be faced in the future. we should design in such a way that we can always add new features to the software without modifying the existing design and then adding some new design to the existing design(this is known as "open close principle").

design patterns and principles do enable the same. these are the guidelines which have been identified by many design gurus. people have made some mistakes in the past about designing a software and later realized that they were going wrong, but they also found out that there are some common solutions which can be followed for such design problems and so evolved design patterns and design principles.

but one thing which we all have to remember is that design patterns should not be used to impress people, it has to be used to make software maintainable.
it really does not make sense to use all the design patterns in design if they are not really needed.

so lets make sure that we use these to the fullest extent and with proper discretion and make our software design extendable.

wait for my posts on specific design patterns.

The problems for which the design patterns are used are also unique.

Thursday, May 16, 2019

Azure Key vaults

what is Azure key vault?

    it is a feature in Azure where keys and their secrets can be stored. for more details refer https://docs.microsoft.com/en-us/azure/key-vault/tutorial-net-create-vault-azure-web-app

instead of keeping all the secrets in config file, the secrets are stored in key vault which is more secure.

Pre-requisites

The pre-requisites have to be set right
1) The app needs permission to access Key Vault (in Azure AD --> App registrations)
2) The key vault needs to provide permission to the app. this can be done by selecting the specifc keyvault in the portal and selecting access policies and adding the application details (like id and secret)

how does my application access key vault?

mechanism to access the key vault

1) Use the clientID and Client secret from the app and access the key vault.
2) use managed service identity from the app service

use client id and client secret:

in this case, the client id and secret have to be in the config file. so, this is the recommended mechanism if client id and secret should not be stored in the config file. however, i have shared the code in the last portion of this blog article.

Managed service identity:

go to azure portal, select the app service which needs to access the key vault and select identities and enable managed identity.
enabling this ensure that whenever an application tries to access the key vault, the client id and client secret need not be explicitly passed and the identity is passed by the application.

use this code to access your key vault:

public async Task<string> GetKey(string key, string keyVaultUri)
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
KeyVaultClient keyVaultClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
var secret = keyVaultClient.GetSecretAsync(keyVaultUri, key).Result;
return secret.Value;
}

caution:

The above piece of code when deployed in azure app service can access the key vault.
but the same code fails in the local development environment.

the reason for this is that on the local development environment, the managed identity is not set.

run azure CLI (2.0.12 or above) and type in az login.
in the login, enter the credentials which has access to the azure subscription where the app service and key vault are present.
 CLI shows a message "you have successfully logged in. let us see all the subscriptions to which you have access"
and then it shows up your subscription.
now run the key vault access code on your local environment, you will be able to access the key vault.



code using client id and secret


private static async Task<string> GetSecretAsync(string vaultUrl, string vaultKey)
{
      var client = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetAccessTokenAsync), new HttpClient());
      var secret = await client.GetSecretAsync(vaultUrl, vaultKey).ConfigureAwait(false);
      return secret.Value;
}

 private static async Task<string> GetAccessTokenAsync(string authority, string resource, string scope)
{
            //DEMO ONLY
            //Storing ApplicationId and Key in code / config file is bad idea :)
            var appCredentials = new ClientCredential("<client id GUID>", "<client secret>");
            var context = new AuthenticationContext(authority, TokenCache.DefaultShared);

            var result = await context.AcquireTokenAsync(resource, appCredentials).ConfigureAwait(false);

            return result.AccessToken;
 }

Thursday, February 28, 2019

CI CD using Azure Devops



To set up a build in Azure devops:

1)    Go to pipelinesà builds



2)    Click on new à new build pipeline
3)    Using the built-in pipelines:
a.     Select the repository type (in EY, it is usually azure repos)
b.     Select the repository
c.     In configure tab, select the pipeline task (.net core, asp.net etc etc)
4)    If you want to configure a custom pipeline:
a.     Select “use the Visual Designer”


b.     Select the repository, source and branch (selecting master branch for builds is the recommended approach)
c.     Click on continue
d.     Pre-existing templates will be displayed. You can select any of these templates (as needed) or select an empty job.


5)    To add individual tasks / activities:
a.     Go to the build pipeline and click on the “+” icon




b.     List of tasks will be displayed.
c.     Add suitable tasks. Usually for .net projects the tasks will be restore, build, test and publish.
d.     “publish artifact” creates zip files with binaries for each project. (this will be useful for deployment as well)
e.     Click on “save and queue”, the build will start. After completion of the build, an email is sent.
f.      To view the build artifacts click on view results in the email.
g.     In the page that is displayed, select artifacts

h.     Given below is a snapshot of asp.net core app.



i.      In the agent job, select “hosted VS 2017” if using VS 2017







To set up a release
1)      Click on pipelines à releases à new release pipeline
2)      In the templates select “azure app service deployment”


3)      A new stage is created. The release pipeline can be split into multiple stages.




4)      In the artifacts, click on add an artifact,

5)      Select source type as build, select the project and also the build pipeline
6)      Click on 1 job,1 task link



7)      Select azure subscription and app service name
8)      In the deploy azure App service task, enter the package or folder details (the zip file from the build artifacts)


9)      Go to application settings and configuration settings to add any app settings



10)  Save and click on release.
11)  The application is deployed to the azure app service.