|
| 1 | +--- |
| 2 | +title: Security |
| 3 | +titleSuffix: Azure Cognitive Services |
| 4 | +description: Learn about the various security considerations for Cognitive Services usage. |
| 5 | +services: cognitive-services |
| 6 | +author: IEvangelist |
| 7 | +manager: nitinme |
| 8 | +ms.service: cognitive-services |
| 9 | +ms.topic: conceptual |
| 10 | +ms.date: 03/18/2020 |
| 11 | +ms.author: dapine |
| 12 | +--- |
| 13 | + |
| 14 | +# Azure Cognitive Services security |
| 15 | + |
| 16 | +Security should be considered a top priority when developing any and all applications. With the onset of artificial intelligence enabled applications, security is even more important. In this article various aspects of Azure Cognitive Services security are outlined, such as the use of transport layer security, authentication, and securely configuring sensitive data. |
| 17 | + |
| 18 | +## Transport Layer Security (TLS) |
| 19 | + |
| 20 | +All of the Cognitive Services endpoints exposed over HTTP enforce TLS 1.2. With an enforced security protocol, consumers attempting to call a Cognitive Services endpoint should adhere to guidelines: |
| 21 | + |
| 22 | +* The client Operating System (OS) would need to support TLS 1.2 |
| 23 | +* The language (and platform) used to make the HTTP call would need to specify TLS 1.2 as part of the request |
| 24 | + * Depending on the language and platform, specifying TLS is done either implicitly or explicitly |
| 25 | + |
| 26 | +For .NET users, consider the <a href="https://docs.microsoft.com/dotnet/framework/network-programming/tls" target="_blank">Transport Layer Security best practices <span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 27 | + |
| 28 | +## Authentication |
| 29 | + |
| 30 | +When discussing authentication, there are several common misconceptions. Authentication and authorization are often confused for one another. Identity is also a major component in security. An identity is a collection of information about a <a href="https://en.wikipedia.org/wiki/Principal_(computer_security)" target="_blank">principal <span class="docon docon-navigate-external x-hidden-focus"></span></a>. Identity providers (IdP) provide identities to authentication services. Authentication is the act of verifying a user's identity. Authorization is the specification of access rights and privileges to resources for a given identity. |
| 31 | + |
| 32 | +For more information on authentication with subscription keys, access tokens and Azure Active Directory (AAD), see <a href="https://docs.microsoft.com/azure/cognitive-services/authentication" target="_blank">authenticate requests to Azure Cognitive Services<span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 33 | + |
| 34 | +## Environment variables and application configuration |
| 35 | + |
| 36 | +Environment variables are name-value pairs, stored within a specific environment. A more secure alternative to using hardcoded values for sensitive data, is to use environment variables. Hardcoded values are insecure and should be avoided. |
| 37 | + |
| 38 | +> [!CAUTION] |
| 39 | +> Do **not** use hardcoded values for sensitive data, doing so is a major security vulnerability. |
| 40 | +
|
| 41 | +> [!NOTE] |
| 42 | +> While environment variables are stored in plain text, they are isolated to an environment. If an environment is compromised, so too are the variables with the environment. |
| 43 | +
|
| 44 | +### Set environment variable |
| 45 | + |
| 46 | +To set environment variables, use one the following commands - where the `ENVIRONMENT_VARIABLE_KEY` is the named key and `value` is the value stored in the environment variable. |
| 47 | + |
| 48 | +# [Command Line](#tab/command-line) |
| 49 | + |
| 50 | +```CMD |
| 51 | +:: Assigns the env var to the value |
| 52 | +set ENVIRONMENT_VARIABLE_KEY=value |
| 53 | +
|
| 54 | +:: Prints the env var value |
| 55 | +echo %ENVIRONMENT_VARIABLE_KEY% |
| 56 | +``` |
| 57 | + |
| 58 | +# [PowerShell](#tab/powershell) |
| 59 | + |
| 60 | +```powershell |
| 61 | +# Assigns the env var to the value |
| 62 | +$Env:ENVIRONMENT_VARIABLE_KEY="value" |
| 63 | +
|
| 64 | +# Prints the env var value |
| 65 | +$Env:ENVIRONMENT_VARIABLE_KEY |
| 66 | +``` |
| 67 | + |
| 68 | +# [Bash](#tab/bash) |
| 69 | + |
| 70 | +```Bash |
| 71 | +# Assigns the env var to the value |
| 72 | +export ENVIRONMENT_VARIABLE_KEY=value |
| 73 | + |
| 74 | +# Prints the env var value |
| 75 | +echo ENVIRONMENT_VARIABLE_KEY |
| 76 | +``` |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +> [!TIP] |
| 81 | +> After setting an environment variable, restart your integrated development environment (IDE) to ensure that newly added environment variables are available. |
| 82 | +
|
| 83 | +### Get environment variable |
| 84 | + |
| 85 | +To get an environment variable, it must be read into memory. Depending on the language you're using, consider the following code snippets. These code snippets demonstrate how to get environment variable given the `ENVIRONMENT_VARIABLE_KEY` and assign to a variable named `value`. |
| 86 | + |
| 87 | +# [C#](#tab/csharp) |
| 88 | + |
| 89 | +For more information, see <a href="https://docs.microsoft.com/dotnet/api/system.environment.getenvironmentvariable" target="_blank">`Environment.GetEnvironmentVariable` <span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 90 | + |
| 91 | +```csharp |
| 92 | +using static System.Environment; |
| 93 | + |
| 94 | +class Program |
| 95 | +{ |
| 96 | + static void Main() |
| 97 | + { |
| 98 | + // Get the named env var, and assign it to the value variable |
| 99 | + var value = |
| 100 | + GetEnvironmentVariable( |
| 101 | + "ENVIRONMENT_VARIABLE_KEY"); |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +# [C++](#tab/cpp) |
| 107 | + |
| 108 | +For more information, see <a href="https://docs.microsoft.com/cpp/c-runtime-library/reference/getenv-wgetenv" target="_blank">`getenv` <span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 109 | + |
| 110 | +```cpp |
| 111 | +#include <stdlib.h> |
| 112 | + |
| 113 | +int main() |
| 114 | +{ |
| 115 | + // Get the named env var, and assign it to the value variable |
| 116 | + auto value = |
| 117 | + getenv("ENVIRONMENT_VARIABLE_KEY"); |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +# [Java](#tab/java) |
| 122 | + |
| 123 | +For more information, see <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getenv(java.lang.String)" target="_blank">`System.getenv` <span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 124 | + |
| 125 | +```java |
| 126 | +import java.lang.*; |
| 127 | + |
| 128 | +public class Program { |
| 129 | + public static void main(String[] args) throws Exception { |
| 130 | + // Get the named env var, and assign it to the value variable |
| 131 | + String value = |
| 132 | + System.getenv( |
| 133 | + "ENVIRONMENT_VARIABLE_KEY") |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +# [Node.js](#tab/node-js) |
| 139 | + |
| 140 | +For more information, see <a href="https://nodejs.org/api/process.html#process_process_env" target="_blank">`process.env` <span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 141 | + |
| 142 | +```javascript |
| 143 | +// Get the named env var, and assign it to the value variable |
| 144 | +const value = |
| 145 | + process.env.ENVIRONMENT_VARIABLE_KEY; |
| 146 | +``` |
| 147 | + |
| 148 | +# [Python](#tab/python) |
| 149 | + |
| 150 | +For more information, see <a href="https://docs.python.org/2/library/os.html#os.environ" target="_blank">`os.environ` <span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 151 | + |
| 152 | +```python |
| 153 | +import os |
| 154 | + |
| 155 | +# Get the named env var, and assign it to the value variable |
| 156 | +value = os.environ['ENVIRONMENT_VARIABLE_KEY'] |
| 157 | +``` |
| 158 | + |
| 159 | +# [Objective-C](#tab/objective-c) |
| 160 | + |
| 161 | +For more information, see <a href="https://developer.apple.com/documentation/foundation/nsprocessinfo/1417911-environment?language=objc" target="_blank">`environment` <span class="docon docon-navigate-external x-hidden-focus"></span></a>. |
| 162 | + |
| 163 | +```objectivec |
| 164 | +// Get the named env var, and assign it to the value variable |
| 165 | +NSString* value = |
| 166 | + [[[NSProcessInfo processInfo]environment]objectForKey:@"ENVIRONMENT_VARIABLE_KEY"]; |
| 167 | +``` |
| 168 | + |
| 169 | +--- |
| 170 | + |
| 171 | +## Next steps |
| 172 | + |
| 173 | +* Explore the various [Cognitive Services](welcome.md) |
| 174 | +* Learn more about [Cognitive Services Virtual Networks](cognitive-services-virtual-networks.md) |
0 commit comments