Chapter 08. Optimizing, Securing, and Maintaining Your DreamFactory Enviroment
The DreamFactory platform is build atop the Laravel. Laravel is an amazing PHP-based framework that in just a few short years has grown in popularity to become one of the today's most popular framework solutions regardless of language. We speculate there are several reasons for such soaring popularity, including a thoroughly pragmatic approach, security-first implementation, fantastic documentation, and a comprehensive ecosystem (in addition to the framework itself, the Laravel development team also maintains an e-commerce framework called Spark, an application adminstration toolkit called Nova, and an application deployment service called Envoyer. Further, it's also quite performant, capable of serving as the backbone for high-traffic, mission critical applications. Regardless, like any application you're going to want to learn all you can about how to best go about maintaining, securing, and optimizing the environment. This chapter tackles all three topics.
Optimizing DreamFactory's Database APIs
Ensuring the DreamFactory-generated database APIs are running at peak performance is accomplished by ensuring your database is properly configured, has been allocated appropriate hardware and network resources, and turning on DreamFactory's database caching feature. In this section we'll talk more about all of these tasks.
Index the Database
For database-backed APIs, there is no more impactful task one could take than properly indexing the database. Database indexing is what allows your database engine to quickly identify which rows match conditions defined by a
where clause. Refer to the following resources for both general and database-specific indexing information:
Database API Caching
Enable database API caching whenever practical at service creation time, as it will undoubtedly improve performance.
You can achieve particularly high performance by compiling your DreamFactory application code using OPcache. You can learn more about OPcache in these links:
DreamFactory instances may be load balanced, and can be configured to share the system database, cache details, and other information necessary to operate in a distributed environment. Below are some links that may help you configure a load balancer with some of the most common cloud providers.
DreamFactory enables file-based caching by default, however you may opt to configure one of the other supported caching solutions, such as Redis. Please see these links to see connection tutorials:
Always make sure your
CORS settings are only set for the appropriate "scheme/host/port tuple" to ensure you are observing the maximum security you can by only allowing cross origin resources access when there is no other way around it. For a great explanation of
CORS and how they work, please see this article.
- You can modify your
CORSsettings in DreamFactory under the
For database-backed APIs, create the API using a database account privileges that closely correspond to your API privilege requirements. For instance, if the database includes a table called
employees but there is no intention for this table to be accessible via the API, then configure the proxy user's privileges accordingly.
Never use a blanket API key for your APIs! Instead, create roles which expressly define the level of privileges intended to be exposed via the API, and then associate the role with a new App and corresponding API Key. Don't be afraid to create multiple roles and therefore multiple corresponding API keys if you'd like to limit API access in different ways on a per-client or group basis.
- Should you need to make API documentation available to team members, use DreamFactory's user-centric role assignment feature to make solely the documentation available to the team members, rather than granting unnecessary administrative access.
Securing Your Web Traffic
From a networking standpoint DreamFactory is a typical web application, meaning you can easily encrypt all web traffic between the platform and client using an SSL certificate. Unless you've already taken steps to add an SSL certificate to your web server, by default your DreamFactory instance will run on port 80, which means all traffic between your DreamFactory server and client will be unencrypted and therefore subject to capture and review. To fix this, you'll want to install an SSL certificate. One of our favorite resources to create SSL certificates is Let's Encrypt.
Below are resources on how to add an SSL cert to your web server:
When running DreamFactory in a production environment, be sure to set the
APP_ENV value to
false. Leaving it set to
local will result in detailed error-related information being returned to the client rather than quietly logged to the log file. When set properly in a production environment, your
.env file will look like this:
... APP_DEBUG=false ## Environment this installation is running in: local, production (default) APP_ENV=production
Separating the Web Administration Interface from the Platform
New DreamFactory users often conflate the web administration interface with the API platform; in fact, the web administration interface is just a client like any other. It just so happens that the DreamFactory team built this particular interface expressly for managing the platform in an administrative capacity. This interface talks to the platform using a series of administrative APIs exposed by the platform, and accessible only when requests are accompanied by a session token associated with an authenticated administrator.
By default this interface runs on the same server as the platform itself. Some users prefer to entirely separate the two, running the interface in one networking environment and entirely isolating the platform in another.
TODO: Add link to df-admin-app README.
Implementing Key Security Safeguards
Obfuscating Sensitive Data
Adding Redis Caching
One of DreamFactory's great advantages is it is built atop Laravel, and as such, you can take advantage of Laravel's support for shared caching solutions, among other things. This is great because it means the caching solution has been extensively tested and proven in production environments.
To install the predis package you just need to navigate to your project's root directory and execute this command:
$ composer require predis/predis
Next, open your .env file and look for this section:
## CACHE_DRIVER options: apc, array, database, file, memcached, redis CACHE_DRIVER=file Change CACHE_DRIVER to: CACHE_DRIVER=redis
Next, scroll down and uncomment these lines by removing the
#, and then update the
CACHE_PORT, and (optionally) the
CACHE_PASSWORD parameters to match your Redis environment:
## If CACHE_DRIVER = memcached or redis #CACHE_HOST= #CACHE_PORT= #CACHE_PASSWORD=
Finally, scroll down to the following section and uncomment
## If CACHE_DRIVER = redis #CACHE_DATABASE=2 ## Which Redis client to use: predis or phpredis (PHP extension) #REDIS_CLIENT=predis
You can probably leave CACHE_DATABASE set to 2. For the
REDIS_CLIENT you can leave it set to predis if you've installed the predis/predis package (recommended).